From a7736d88a925b9591df62847359b2ed5b2cd787d Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 4 Jul 2021 15:44:25 +0200 Subject: [PATCH] got rid of Module.isLibraryModule variable, is now function that derives it from source path --- .../src/prog8/optimizer/UnusedCodeRemover.kt | 6 +++--- compiler/test/AsmgenTests.kt | 2 +- compiler/test/TestMemory.kt | 14 +++++++------- compilerAst/src/prog8/ast/AstToSourceCode.kt | 2 +- compilerAst/src/prog8/ast/AstToplevel.kt | 17 ++++++++++++++--- compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt | 6 +++--- compilerAst/src/prog8/parser/ModuleParsing.kt | 8 ++++---- compilerAst/test/TestAntlrParser.kt | 2 +- 8 files changed, 34 insertions(+), 23 deletions(-) diff --git a/compiler/src/prog8/optimizer/UnusedCodeRemover.kt b/compiler/src/prog8/optimizer/UnusedCodeRemover.kt index 6493aa7ae..32f0ae010 100644 --- a/compiler/src/prog8/optimizer/UnusedCodeRemover.kt +++ b/compiler/src/prog8/optimizer/UnusedCodeRemover.kt @@ -20,7 +20,7 @@ internal class UnusedCodeRemover(private val program: Program, private val callgraph = CallGraph(program) override fun before(module: Module, parent: Node): Iterable { - return if (!module.isLibraryModule && (module.containsNoCodeNorVars() || callgraph.unused(module))) + return if (!module.isLibrary() && (module.containsNoCodeNorVars() || callgraph.unused(module))) listOf(IAstModification.Remove(module, module.definingScope())) else noModifications @@ -80,12 +80,12 @@ internal class UnusedCodeRemover(private val program: Program, val forceOutput = "force_output" in subroutine.definingBlock().options() if (subroutine !== program.entrypoint() && !forceOutput && !subroutine.inline && !subroutine.isAsmSubroutine) { if(callgraph.unused(subroutine)) { - if(!subroutine.definingModule().isLibraryModule) + if(!subroutine.definingModule().isLibrary()) errors.warn("removing unused subroutine '${subroutine.name}'", subroutine.position) return listOf(IAstModification.Remove(subroutine, subroutine.definingScope())) } if(subroutine.containsNoCodeNorVars()) { - if(!subroutine.definingModule().isLibraryModule) + if(!subroutine.definingModule().isLibrary()) errors.warn("removing empty subroutine '${subroutine.name}'", subroutine.position) val removals = mutableListOf(IAstModification.Remove(subroutine, subroutine.definingScope())) callgraph.calledBy[subroutine]?.let { diff --git a/compiler/test/AsmgenTests.kt b/compiler/test/AsmgenTests.kt index 6cd6ac808..8deeae874 100644 --- a/compiler/test/AsmgenTests.kt +++ b/compiler/test/AsmgenTests.kt @@ -74,7 +74,7 @@ locallabel: val varInBlock = VarDecl(VarDeclType.VAR, DataType.UWORD, ZeropageWish.DONTCARE, null, "var_outside", null, false, false, false, Position.DUMMY) val block = Block("main", null, mutableListOf(labelInBlock, varInBlock, subroutine), false, Position.DUMMY) - val module = Module("test", mutableListOf(block), Position.DUMMY, false, Path.of("")) + val module = Module("test", mutableListOf(block), Position.DUMMY, Path.of("")) module.linkParents(ParentSentinel) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer()) module.program = program diff --git a/compiler/test/TestMemory.kt b/compiler/test/TestMemory.kt index 0c266848a..b9ca856d5 100644 --- a/compiler/test/TestMemory.kt +++ b/compiler/test/TestMemory.kt @@ -98,7 +98,7 @@ class TestMemory { val target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY) val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", emptyList(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) - val module = Module("test", mutableListOf(subroutine), Position.DUMMY, false, Path.of("")) + val module = Module("test", mutableListOf(subroutine), Position.DUMMY, Path.of("")) module.linkParents(ParentSentinel) return target } @@ -117,7 +117,7 @@ class TestMemory { val target = AssignTarget(IdentifierReference(listOf("address"), Position.DUMMY), null, null, Position.DUMMY) val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", emptyList(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) - val module = Module("test", mutableListOf(subroutine), Position.DUMMY, false, Path.of("")) + val module = Module("test", mutableListOf(subroutine), Position.DUMMY, Path.of("")) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer()) module.linkParents(ParentSentinel) assertTrue(C64Target.isInRegularRAM(target, program)) @@ -130,7 +130,7 @@ class TestMemory { val target = AssignTarget(IdentifierReference(listOf("address"), Position.DUMMY), null, null, Position.DUMMY) val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", emptyList(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) - val module = Module("test", mutableListOf(subroutine), Position.DUMMY, false, Path.of("")) + val module = Module("test", mutableListOf(subroutine), Position.DUMMY, Path.of("")) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer()) module.linkParents(ParentSentinel) assertTrue(C64Target.isInRegularRAM(target, program)) @@ -143,7 +143,7 @@ class TestMemory { val target = AssignTarget(IdentifierReference(listOf("address"), Position.DUMMY), null, null, Position.DUMMY) val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", emptyList(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) - val module = Module("test", mutableListOf(subroutine), Position.DUMMY, false, Path.of("")) + val module = Module("test", mutableListOf(subroutine), Position.DUMMY, Path.of("")) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer()) module.linkParents(ParentSentinel) assertFalse(C64Target.isInRegularRAM(target, program)) @@ -156,7 +156,7 @@ class TestMemory { val target = AssignTarget(null, arrayindexed, null, Position.DUMMY) val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", emptyList(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) - val module = Module("test", mutableListOf(subroutine), Position.DUMMY, false, Path.of("")) + val module = Module("test", mutableListOf(subroutine), Position.DUMMY, Path.of("")) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer()) module.linkParents(ParentSentinel) assertTrue(C64Target.isInRegularRAM(target, program)) @@ -170,7 +170,7 @@ class TestMemory { val target = AssignTarget(null, arrayindexed, null, Position.DUMMY) val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", emptyList(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) - val module = Module("test", mutableListOf(subroutine), Position.DUMMY, false, Path.of("")) + val module = Module("test", mutableListOf(subroutine), Position.DUMMY, Path.of("")) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer()) module.linkParents(ParentSentinel) assertTrue(C64Target.isInRegularRAM(target, program)) @@ -184,7 +184,7 @@ class TestMemory { val target = AssignTarget(null, arrayindexed, null, Position.DUMMY) val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY) val subroutine = Subroutine("test", emptyList(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY) - val module = Module("test", mutableListOf(subroutine), Position.DUMMY, false, Path.of("")) + val module = Module("test", mutableListOf(subroutine), Position.DUMMY, Path.of("")) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer()) module.linkParents(ParentSentinel) assertFalse(C64Target.isInRegularRAM(target, program)) diff --git a/compilerAst/src/prog8/ast/AstToSourceCode.kt b/compilerAst/src/prog8/ast/AstToSourceCode.kt index dd70c53bc..71fb3d7a5 100644 --- a/compilerAst/src/prog8/ast/AstToSourceCode.kt +++ b/compilerAst/src/prog8/ast/AstToSourceCode.kt @@ -24,7 +24,7 @@ class AstToSourceCode(val output: (text: String) -> Unit, val program: Program): } override fun visit(module: Module) { - if(!module.isLibraryModule) { + if(!module.isLibrary()) { outputln("; ----------- module: ${module.name} -----------") super.visit(module) } diff --git a/compilerAst/src/prog8/ast/AstToplevel.kt b/compilerAst/src/prog8/ast/AstToplevel.kt index 3e53e2e3b..216d0bcb5 100644 --- a/compilerAst/src/prog8/ast/AstToplevel.kt +++ b/compilerAst/src/prog8/ast/AstToplevel.kt @@ -6,6 +6,8 @@ import prog8.ast.statements.* import prog8.ast.walk.AstWalker import prog8.ast.walk.IAstVisitor import java.nio.file.Path +import java.nio.file.Paths +import kotlin.io.path.name import kotlin.math.abs const val internedStringsModuleName = "prog8_interned_strings" @@ -263,7 +265,7 @@ class Program(val name: String, init { // insert a container module for all interned strings later if(modules.firstOrNull()?.name != internedStringsModuleName) { - val internedStringsModule = Module(internedStringsModuleName, mutableListOf(), Position.DUMMY, true, Path.of("")) + val internedStringsModule = Module(internedStringsModuleName, mutableListOf(), Position.DUMMY, Path.of("")) modules.add(0, internedStringsModule) val block = Block(internedStringsModuleName, null, mutableListOf(), true, Position.DUMMY) internedStringsModule.statements.add(block) @@ -340,7 +342,6 @@ class Program(val name: String, class Module(override val name: String, override var statements: MutableList, override val position: Position, - val isLibraryModule: Boolean, val source: Path) : Node, INameScope { override lateinit var parent: Node @@ -364,10 +365,20 @@ class Module(override val name: String, replacement.parent = this } - override fun toString() = "Module(name=$name, pos=$position, lib=$isLibraryModule)" + override fun toString() = "Module(name=$name, pos=$position, lib=${isLibrary()})" fun accept(visitor: IAstVisitor) = visitor.visit(this) fun accept(visitor: AstWalker, parent: Node) = visitor.visit(this, parent) + + companion object { + fun pathForResource(resourcePath: String): Path { + return Paths.get("@embedded@/$resourcePath") + } + + fun isLibrary(source: Path) = source.name=="" || source.startsWith("@embedded@/") + } + + fun isLibrary() = isLibrary(source) } diff --git a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt index efe9f81a7..fc07eece1 100644 --- a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt +++ b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt @@ -19,11 +19,11 @@ import java.nio.file.Path private data class NumericLiteral(val number: Number, val datatype: DataType) -internal fun prog8Parser.ModuleContext.toAst(name: String, isLibrary: Boolean, source: Path, encoding: IStringEncoding) : Module { +internal fun prog8Parser.ModuleContext.toAst(name: String, source: Path, encoding: IStringEncoding) : Module { val nameWithoutSuffix = if(name.endsWith(".p8")) name.substringBeforeLast('.') else name val directives = this.directive().map { it.toAst() } - val blocks = this.block().map { it.toAst(isLibrary, encoding) } - return Module(nameWithoutSuffix, (directives + blocks).toMutableList(), toPosition(), isLibrary, source) + val blocks = this.block().map { it.toAst(Module.isLibrary(source), encoding) } + return Module(nameWithoutSuffix, (directives + blocks).toMutableList(), toPosition(), source) } private fun ParserRuleContext.toPosition() : Position { diff --git a/compilerAst/src/prog8/parser/ModuleParsing.kt b/compilerAst/src/prog8/parser/ModuleParsing.kt index efae7049e..94faa6ee4 100644 --- a/compilerAst/src/prog8/parser/ModuleParsing.kt +++ b/compilerAst/src/prog8/parser/ModuleParsing.kt @@ -44,7 +44,7 @@ class ModuleImporter(private val program: Program, if(!Files.isReadable(filePath)) throw ParsingFailedError("No such file: $filePath") - return importModule(CharStreams.fromPath(filePath), filePath, false) + return importModule(CharStreams.fromPath(filePath), filePath) } fun importLibraryModule(name: String): Module? { @@ -68,7 +68,7 @@ class ModuleImporter(private val program: Program, } } - private fun importModule(stream: CharStream, modulePath: Path, isLibrary: Boolean): Module { + private fun importModule(stream: CharStream, modulePath: Path): Module { val moduleName = moduleName(modulePath.fileName) val lexer = CustomLexer(modulePath, stream) lexer.removeErrorListeners() @@ -87,7 +87,7 @@ class ModuleImporter(private val program: Program, // tokens.commentTokens().forEach { println(it) } // convert to Ast - val moduleAst = parseTree.toAst(moduleName, isLibrary, modulePath, encoder) + val moduleAst = parseTree.toAst(moduleName, modulePath, encoder) moduleAst.program = program moduleAst.linkParents(program.namespace) program.modules.add(moduleAst) @@ -122,7 +122,7 @@ class ModuleImporter(private val program: Program, resource.use { println("importing '$moduleName' (library)") val content = it.reader().readText().replace("\r\n", "\n") - importModule(CharStreams.fromString(content), Paths.get("@embedded@/$resourcePath"), true) + importModule(CharStreams.fromString(content), Module.pathForResource(resourcePath)) } } else { val modulePath = tryGetModuleFromFile(moduleName, source, import.position) diff --git a/compilerAst/test/TestAntlrParser.kt b/compilerAst/test/TestAntlrParser.kt index 4cdef99fa..bb90bfb34 100644 --- a/compilerAst/test/TestAntlrParser.kt +++ b/compilerAst/test/TestAntlrParser.kt @@ -249,7 +249,7 @@ main { // parser.removeErrorListeners() // parser.addErrorListener(MyErrorListener()) - val ast = parser.module().toAst("test", false, Path.of(""), DummyEncoding) + val ast = parser.module().toAst("test", Path.of(""), DummyEncoding) assertIs(ast.statements.first()) assertEquals((ast.statements.first() as Block).name, "main") }