1
0
mirror of https://github.com/fluencelabs/asmble synced 2025-03-16 11:10:52 +00:00

Removed prefixed dollar sign from sexpr names and add export names to dedupe check for issue

This commit is contained in:
Chad Retz 2018-07-25 16:30:48 -05:00
parent 1d5c1e527a
commit 706da0d486
6 changed files with 27 additions and 21 deletions
compiler/src
main/kotlin/asmble
test/kotlin/asmble

@ -40,11 +40,20 @@ data class ClsContext(
mod.tables.isNotEmpty() || mod.imports.any { it.kind is Node.Import.Kind.Table }
}
val dedupedFuncNames: Map<Int, String>? by lazy {
val seen = mutableSetOf<String>()
// 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,

@ -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<SExpr>?) =
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<SExpr.Multi>.unwrapInstrs() =
if (parensInstrs) this else this.single().vals
private val String.quoted get() = fromString(this, true)

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

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

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

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