Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Syntactically separate params from locals in Function.
Browse files Browse the repository at this point in the history
This is closer to the WebAssembly structure, where params and
results are part of the `typeuse` while locals are distinct. It
is also easier to manipulate in the text and binary writers.
  • Loading branch information
sjrd committed May 17, 2024
1 parent 222df3a commit 3b7cbf8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,12 @@ class BinaryWriter(module: Module, emitDebugInfo: Boolean) {
private def writeFunc(buf: Buffer, func: Function): Unit = {
emitStartFuncPosition(buf, func.pos)

buf.vec(func.locals.filter(!_.isParameter)) { local =>
buf.vec(func.locals) { local =>
buf.u32(1)
writeType(buf, local.typ)
}

withLocalIdxValues(func.locals.map(_.name).zipWithIndex.toMap) {
withLocalIdxValues((func.params ::: func.locals).map(_.name).zipWithIndex.toMap) {
writeExpr(buf, func.body)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class FunctionBuilder(
setResultTypes(typ :: Nil)

def addParam(name: LocalName, typ: Type): LocalName = {
params += Local(name, typ, isParameter = true)
params += Local(name, typ)
name
}

Expand All @@ -51,7 +51,7 @@ final class FunctionBuilder(
}

def addLocal(name: LocalName, typ: Type): LocalName = {
locals += Local(name, typ, isParameter = false)
locals += Local(name, typ)
name
}

Expand Down Expand Up @@ -304,10 +304,15 @@ final class FunctionBuilder(

val dcedInstrs = localDeadCodeEliminationOfInstrs()

val expr = Expr(dcedInstrs)
val allLocals = params.prependToList(locals.toList)
val func =
Function(functionName, functionTypeName, allLocals, resultTypes, expr, functionPos)
val func = Function(
functionName,
functionTypeName,
params.toList,
resultTypes,
locals.toList,
Expr(dcedInstrs),
functionPos
)
moduleBuilder.addFunction(func)
func
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,18 @@ object Modules {
final case class Tag(id: TagName, typeName: TypeName) extends ImportDesc
}

/** @see
* https://webassembly.github.io/spec/core/syntax/modules.html#functions
*/
/** A WebAssembly `func`, including names for parameters and locals. */
final case class Function(
val name: FunctionName,
val typeName: TypeName,
val locals: List[Local],
val results: List[Type],
val body: Expr,
val pos: Position
name: FunctionName,
typeName: TypeName,
params: List[Local],
results: List[Type],
locals: List[Local],
body: Expr,
pos: Position
)

/** The index space for locals is only accessible inside a function and includes the parameters of
* that function, which precede the local variables.
*/
case class Local(
val name: LocalName,
val typ: Type,
val isParameter: Boolean // for text
)
final case class Local(name: LocalName, typ: Type)

final case class Tag(val name: TagName, val typ: TypeName)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,15 @@ class TextWriter {

b.newLineList(
"func", {
val (params, nonParams) = f.locals.partition(_.isParameter)
b.appendName(f.name)
writeTypeUse(f.typeName)

b.newLine()
params.foreach(writeParam)
f.params.foreach(writeParam)
f.results.foreach(r => { b.sameLineList("result", writeType(r)) })

b.newLine()
if (nonParams.nonEmpty) {
nonParams.foreach(writeLocal)
}
f.locals.foreach(writeLocal)
f.body.instr.foreach(writeInstr)
}
)
Expand Down

0 comments on commit 3b7cbf8

Please sign in to comment.