got rid of Module.isLibraryModule variable, is now function that derives it from source path

This commit is contained in:
Irmen de Jong 2021-07-04 15:44:25 +02:00
parent 049dbf5a78
commit a7736d88a9
8 changed files with 34 additions and 23 deletions

View File

@ -20,7 +20,7 @@ internal class UnusedCodeRemover(private val program: Program,
private val callgraph = CallGraph(program) private val callgraph = CallGraph(program)
override fun before(module: Module, parent: Node): Iterable<IAstModification> { override fun before(module: Module, parent: Node): Iterable<IAstModification> {
return if (!module.isLibraryModule && (module.containsNoCodeNorVars() || callgraph.unused(module))) return if (!module.isLibrary() && (module.containsNoCodeNorVars() || callgraph.unused(module)))
listOf(IAstModification.Remove(module, module.definingScope())) listOf(IAstModification.Remove(module, module.definingScope()))
else else
noModifications noModifications
@ -80,12 +80,12 @@ internal class UnusedCodeRemover(private val program: Program,
val forceOutput = "force_output" in subroutine.definingBlock().options() val forceOutput = "force_output" in subroutine.definingBlock().options()
if (subroutine !== program.entrypoint() && !forceOutput && !subroutine.inline && !subroutine.isAsmSubroutine) { if (subroutine !== program.entrypoint() && !forceOutput && !subroutine.inline && !subroutine.isAsmSubroutine) {
if(callgraph.unused(subroutine)) { if(callgraph.unused(subroutine)) {
if(!subroutine.definingModule().isLibraryModule) if(!subroutine.definingModule().isLibrary())
errors.warn("removing unused subroutine '${subroutine.name}'", subroutine.position) errors.warn("removing unused subroutine '${subroutine.name}'", subroutine.position)
return listOf(IAstModification.Remove(subroutine, subroutine.definingScope())) return listOf(IAstModification.Remove(subroutine, subroutine.definingScope()))
} }
if(subroutine.containsNoCodeNorVars()) { if(subroutine.containsNoCodeNorVars()) {
if(!subroutine.definingModule().isLibraryModule) if(!subroutine.definingModule().isLibrary())
errors.warn("removing empty subroutine '${subroutine.name}'", subroutine.position) errors.warn("removing empty subroutine '${subroutine.name}'", subroutine.position)
val removals = mutableListOf(IAstModification.Remove(subroutine, subroutine.definingScope())) val removals = mutableListOf(IAstModification.Remove(subroutine, subroutine.definingScope()))
callgraph.calledBy[subroutine]?.let { callgraph.calledBy[subroutine]?.let {

View File

@ -74,7 +74,7 @@ locallabel:
val varInBlock = VarDecl(VarDeclType.VAR, DataType.UWORD, ZeropageWish.DONTCARE, null, "var_outside", null, false, false, false, Position.DUMMY) 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 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) module.linkParents(ParentSentinel)
val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer()) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer())
module.program = program module.program = program

View File

@ -98,7 +98,7 @@ class TestMemory {
val target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY) val target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, 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 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) module.linkParents(ParentSentinel)
return target return target
} }
@ -117,7 +117,7 @@ class TestMemory {
val target = AssignTarget(IdentifierReference(listOf("address"), Position.DUMMY), null, null, Position.DUMMY) val target = AssignTarget(IdentifierReference(listOf("address"), Position.DUMMY), null, null, Position.DUMMY)
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, 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 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()) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer())
module.linkParents(ParentSentinel) module.linkParents(ParentSentinel)
assertTrue(C64Target.isInRegularRAM(target, program)) assertTrue(C64Target.isInRegularRAM(target, program))
@ -130,7 +130,7 @@ class TestMemory {
val target = AssignTarget(IdentifierReference(listOf("address"), Position.DUMMY), null, null, Position.DUMMY) val target = AssignTarget(IdentifierReference(listOf("address"), Position.DUMMY), null, null, Position.DUMMY)
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, 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 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()) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer())
module.linkParents(ParentSentinel) module.linkParents(ParentSentinel)
assertTrue(C64Target.isInRegularRAM(target, program)) assertTrue(C64Target.isInRegularRAM(target, program))
@ -143,7 +143,7 @@ class TestMemory {
val target = AssignTarget(IdentifierReference(listOf("address"), Position.DUMMY), null, null, Position.DUMMY) val target = AssignTarget(IdentifierReference(listOf("address"), Position.DUMMY), null, null, Position.DUMMY)
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, 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 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()) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer())
module.linkParents(ParentSentinel) module.linkParents(ParentSentinel)
assertFalse(C64Target.isInRegularRAM(target, program)) assertFalse(C64Target.isInRegularRAM(target, program))
@ -156,7 +156,7 @@ class TestMemory {
val target = AssignTarget(null, arrayindexed, null, Position.DUMMY) val target = AssignTarget(null, arrayindexed, null, Position.DUMMY)
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, 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 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()) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer())
module.linkParents(ParentSentinel) module.linkParents(ParentSentinel)
assertTrue(C64Target.isInRegularRAM(target, program)) assertTrue(C64Target.isInRegularRAM(target, program))
@ -170,7 +170,7 @@ class TestMemory {
val target = AssignTarget(null, arrayindexed, null, Position.DUMMY) val target = AssignTarget(null, arrayindexed, null, Position.DUMMY)
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, 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 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()) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer())
module.linkParents(ParentSentinel) module.linkParents(ParentSentinel)
assertTrue(C64Target.isInRegularRAM(target, program)) assertTrue(C64Target.isInRegularRAM(target, program))
@ -184,7 +184,7 @@ class TestMemory {
val target = AssignTarget(null, arrayindexed, null, Position.DUMMY) val target = AssignTarget(null, arrayindexed, null, Position.DUMMY)
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, 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 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()) val program = Program("test", mutableListOf(module), DummyFunctions(), DummyMemsizer())
module.linkParents(ParentSentinel) module.linkParents(ParentSentinel)
assertFalse(C64Target.isInRegularRAM(target, program)) assertFalse(C64Target.isInRegularRAM(target, program))

View File

@ -24,7 +24,7 @@ class AstToSourceCode(val output: (text: String) -> Unit, val program: Program):
} }
override fun visit(module: Module) { override fun visit(module: Module) {
if(!module.isLibraryModule) { if(!module.isLibrary()) {
outputln("; ----------- module: ${module.name} -----------") outputln("; ----------- module: ${module.name} -----------")
super.visit(module) super.visit(module)
} }

View File

@ -6,6 +6,8 @@ import prog8.ast.statements.*
import prog8.ast.walk.AstWalker import prog8.ast.walk.AstWalker
import prog8.ast.walk.IAstVisitor import prog8.ast.walk.IAstVisitor
import java.nio.file.Path import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.name
import kotlin.math.abs import kotlin.math.abs
const val internedStringsModuleName = "prog8_interned_strings" const val internedStringsModuleName = "prog8_interned_strings"
@ -263,7 +265,7 @@ class Program(val name: String,
init { init {
// insert a container module for all interned strings later // insert a container module for all interned strings later
if(modules.firstOrNull()?.name != internedStringsModuleName) { 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) modules.add(0, internedStringsModule)
val block = Block(internedStringsModuleName, null, mutableListOf(), true, Position.DUMMY) val block = Block(internedStringsModuleName, null, mutableListOf(), true, Position.DUMMY)
internedStringsModule.statements.add(block) internedStringsModule.statements.add(block)
@ -340,7 +342,6 @@ class Program(val name: String,
class Module(override val name: String, class Module(override val name: String,
override var statements: MutableList<Statement>, override var statements: MutableList<Statement>,
override val position: Position, override val position: Position,
val isLibraryModule: Boolean,
val source: Path) : Node, INameScope { val source: Path) : Node, INameScope {
override lateinit var parent: Node override lateinit var parent: Node
@ -364,10 +365,20 @@ class Module(override val name: String,
replacement.parent = this 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: IAstVisitor) = visitor.visit(this)
fun accept(visitor: AstWalker, parent: Node) = visitor.visit(this, parent) 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)
} }

View File

@ -19,11 +19,11 @@ import java.nio.file.Path
private data class NumericLiteral(val number: Number, val datatype: DataType) 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 nameWithoutSuffix = if(name.endsWith(".p8")) name.substringBeforeLast('.') else name
val directives = this.directive().map { it.toAst() } val directives = this.directive().map { it.toAst() }
val blocks = this.block().map { it.toAst(isLibrary, encoding) } val blocks = this.block().map { it.toAst(Module.isLibrary(source), encoding) }
return Module(nameWithoutSuffix, (directives + blocks).toMutableList(), toPosition(), isLibrary, source) return Module(nameWithoutSuffix, (directives + blocks).toMutableList(), toPosition(), source)
} }
private fun ParserRuleContext.toPosition() : Position { private fun ParserRuleContext.toPosition() : Position {

View File

@ -44,7 +44,7 @@ class ModuleImporter(private val program: Program,
if(!Files.isReadable(filePath)) if(!Files.isReadable(filePath))
throw ParsingFailedError("No such file: $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? { 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 moduleName = moduleName(modulePath.fileName)
val lexer = CustomLexer(modulePath, stream) val lexer = CustomLexer(modulePath, stream)
lexer.removeErrorListeners() lexer.removeErrorListeners()
@ -87,7 +87,7 @@ class ModuleImporter(private val program: Program,
// tokens.commentTokens().forEach { println(it) } // tokens.commentTokens().forEach { println(it) }
// convert to Ast // convert to Ast
val moduleAst = parseTree.toAst(moduleName, isLibrary, modulePath, encoder) val moduleAst = parseTree.toAst(moduleName, modulePath, encoder)
moduleAst.program = program moduleAst.program = program
moduleAst.linkParents(program.namespace) moduleAst.linkParents(program.namespace)
program.modules.add(moduleAst) program.modules.add(moduleAst)
@ -122,7 +122,7 @@ class ModuleImporter(private val program: Program,
resource.use { resource.use {
println("importing '$moduleName' (library)") println("importing '$moduleName' (library)")
val content = it.reader().readText().replace("\r\n", "\n") 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 { } else {
val modulePath = tryGetModuleFromFile(moduleName, source, import.position) val modulePath = tryGetModuleFromFile(moduleName, source, import.position)

View File

@ -249,7 +249,7 @@ main {
// parser.removeErrorListeners() // parser.removeErrorListeners()
// parser.addErrorListener(MyErrorListener()) // parser.addErrorListener(MyErrorListener())
val ast = parser.module().toAst("test", false, Path.of(""), DummyEncoding) val ast = parser.module().toAst("test", Path.of(""), DummyEncoding)
assertIs<Block>(ast.statements.first()) assertIs<Block>(ast.statements.first())
assertEquals((ast.statements.first() as Block).name, "main") assertEquals((ast.statements.first() as Block).name, "main")
} }