From 706da0d48600163b1452fa1c74b6b3dd2225ae44 Mon Sep 17 00:00:00 2001 From: Chad Retz Date: Wed, 25 Jul 2018 16:30:48 -0500 Subject: [PATCH] Removed prefixed dollar sign from sexpr names and add export names to dedupe check for issue #17 --- .../kotlin/asmble/compile/jvm/ClsContext.kt | 17 +++++++++++++---- .../src/main/kotlin/asmble/io/AstToSExpr.kt | 6 ++---- .../src/main/kotlin/asmble/io/BinaryToAst.kt | 3 +-- .../src/main/kotlin/asmble/io/SExprToAst.kt | 6 +++--- .../test/kotlin/asmble/compile/jvm/NamesTest.kt | 4 ++-- compiler/src/test/kotlin/asmble/io/NamesTest.kt | 12 ++++++------ 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/compiler/src/main/kotlin/asmble/compile/jvm/ClsContext.kt b/compiler/src/main/kotlin/asmble/compile/jvm/ClsContext.kt index 3eb996c..8fecdca 100644 --- a/compiler/src/main/kotlin/asmble/compile/jvm/ClsContext.kt +++ b/compiler/src/main/kotlin/asmble/compile/jvm/ClsContext.kt @@ -40,11 +40,20 @@ data class ClsContext( mod.tables.isNotEmpty() || mod.imports.any { it.kind is Node.Import.Kind.Table } } val dedupedFuncNames: Map? by lazy { - val seen = mutableSetOf() + // Consider all exports as seen + val seen = mod.exports.flatMap { export -> + when { + export.kind == Node.ExternalKind.FUNCTION -> listOf(export.field.javaIdent) + // Just to make it easy, consider all globals as having setters + export.kind == Node.ExternalKind.GLOBAL -> + export.field.javaIdent.capitalize().let { listOf("get$it", "set$it") } + else -> listOf("get" + export.field.javaIdent.capitalize()) + } + }.toMutableSet() mod.names?.funcNames?.toList()?.sortedBy { it.first }?.map { (index, origName) -> - var name = origName + var name = origName.javaIdent var nameIndex = 0 - while (!seen.add(name)) name = origName + (nameIndex++) + while (!seen.add(name)) name = origName.javaIdent + (nameIndex++) index to name }?.toMap() } @@ -80,7 +89,7 @@ data class ClsContext( fun importGlobalGetterFieldName(index: Int) = "import\$get" + globalName(index) fun importGlobalSetterFieldName(index: Int) = "import\$set" + globalName(index) fun globalName(index: Int) = "\$global$index" - fun funcName(index: Int) = dedupedFuncNames?.get(index)?.javaIdent ?: "\$func$index" + fun funcName(index: Int) = dedupedFuncNames?.get(index) ?: "\$func$index" private fun syntheticFunc( nameSuffix: String, diff --git a/compiler/src/main/kotlin/asmble/io/AstToSExpr.kt b/compiler/src/main/kotlin/asmble/io/AstToSExpr.kt index 6c2c786..ef83955 100644 --- a/compiler/src/main/kotlin/asmble/io/AstToSExpr.kt +++ b/compiler/src/main/kotlin/asmble/io/AstToSExpr.kt @@ -258,10 +258,8 @@ open class AstToSExpr(val parensInstrs: Boolean = true) { if (exp == null) this else this.copy(vals = this.vals + exp) private operator fun SExpr.Multi.plus(exps: List?) = if (exps == null || exps.isEmpty()) this else this.copy(vals = this.vals + exps) - private fun newMulti(initSymb: String? = null, initName: String? = null): SExpr.Multi { - initName?.also { require(it.startsWith("$")) } - return SExpr.Multi() + initSymb + initName - } + private fun newMulti(initSymb: String? = null, initName: String? = null) = + SExpr.Multi() + initSymb + initName?.let { "$$it" } private fun List.unwrapInstrs() = if (parensInstrs) this else this.single().vals private val String.quoted get() = fromString(this, true) diff --git a/compiler/src/main/kotlin/asmble/io/BinaryToAst.kt b/compiler/src/main/kotlin/asmble/io/BinaryToAst.kt index 6c87536..e93991c 100644 --- a/compiler/src/main/kotlin/asmble/io/BinaryToAst.kt +++ b/compiler/src/main/kotlin/asmble/io/BinaryToAst.kt @@ -217,8 +217,7 @@ open class BinaryToAst( } // Try to parse the name section val section = toCustomSection(b, afterSectionId).takeIf { section -> - val shouldParseNames = includeNameSection && customSections.isEmpty() && - nameSection == null && section.name == "name" + val shouldParseNames = includeNameSection && nameSection == null && section.name == "name" !shouldParseNames || try { nameSection = toNameSection(ByteReader.InputStream(section.payload.inputStream())) false diff --git a/compiler/src/main/kotlin/asmble/io/SExprToAst.kt b/compiler/src/main/kotlin/asmble/io/SExprToAst.kt index f905c33..0dd92b9 100644 --- a/compiler/src/main/kotlin/asmble/io/SExprToAst.kt +++ b/compiler/src/main/kotlin/asmble/io/SExprToAst.kt @@ -934,7 +934,7 @@ open class SExprToAst( fun toVarMaybe(exp: SExpr, nameMap: NameMap, nameType: String): Int? { return exp.symbolStr()?.let { it -> if (it.startsWith("$")) - nameMap.get(nameType, it) ?: + nameMap.get(nameType, it.drop(1)) ?: throw Exception("Unable to find index for name $it of type $nameType in $nameMap") else if (it.startsWith("0x")) it.substring(2).toIntOrNull(16) else it.toIntOrNull() @@ -1029,7 +1029,7 @@ open class SExprToAst( private fun SExpr.Multi.maybeName(index: Int): String? { if (this.vals.size > index && this.vals[index] is SExpr.Symbol) { val sym = this.vals[index] as SExpr.Symbol - if (!sym.quoted && sym.contents[0] == '$') return sym.contents + if (!sym.quoted && sym.contents[0] == '$') return sym.contents.drop(1) } return null } @@ -1069,7 +1069,7 @@ open class SExprToAst( fun get(type: String, name: String) = names["$type:$name"] fun getAllNamesByIndex(type: String) = names.mapNotNull { (k, v) -> - k.indexOf(':').takeIf { it != -1 }?.let { v to k.substring(it + 1) } + k.takeIf { k.startsWith("$type:") }?.let { v to k.substring(type.length + 1) } }.toMap() } diff --git a/compiler/src/test/kotlin/asmble/compile/jvm/NamesTest.kt b/compiler/src/test/kotlin/asmble/compile/jvm/NamesTest.kt index 56826d0..51f327e 100644 --- a/compiler/src/test/kotlin/asmble/compile/jvm/NamesTest.kt +++ b/compiler/src/test/kotlin/asmble/compile/jvm/NamesTest.kt @@ -32,7 +32,7 @@ class NamesTest : TestBase() { AstToAsm.fromModule(ctx) val cls = ScriptContext.SimpleClassLoader(javaClass.classLoader, logger).fromBuiltContext(ctx) // Make sure the import field and the func are present named - cls.getDeclaredField("\$import_func") - cls.getDeclaredMethod("\$some_func", Integer.TYPE) + cls.getDeclaredField("import_func") + cls.getDeclaredMethod("some_func", Integer.TYPE) } } \ No newline at end of file diff --git a/compiler/src/test/kotlin/asmble/io/NamesTest.kt b/compiler/src/test/kotlin/asmble/io/NamesTest.kt index d9b286d..3937073 100644 --- a/compiler/src/test/kotlin/asmble/io/NamesTest.kt +++ b/compiler/src/test/kotlin/asmble/io/NamesTest.kt @@ -21,16 +21,16 @@ class NamesTest { ) """.trimIndent())) val expected = Node.NameSection( - moduleName = "\$mod_name", + moduleName = "mod_name", funcNames = mapOf( - 0 to "\$import_func", - 1 to "\$some_func" + 0 to "import_func", + 1 to "some_func" ), localNames = mapOf( 1 to mapOf( - 0 to "\$func_param", - 1 to "\$func_local0", - 2 to "\$func_local1" + 0 to "func_param", + 1 to "func_local0", + 2 to "func_local1" ) ) )