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

Commit

Permalink
Merge FunctionSignature and FunctionType.
Browse files Browse the repository at this point in the history
They were isomorphic anyway, and the Wasm spec only has a notion
of function type, even where we previously used function signature.
  • Loading branch information
sjrd committed May 17, 2024
1 parent 0e0a8be commit c5e4d17
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
* If `expr` is `undefined`, it would be `(1 << 4) == 0b00010000`, which
* would give `false`.
*/
val anyRefToVoidSig =
wamod.FunctionSignature(List(watpe.RefType.anyref), Nil)
val anyRefToVoidSig = wamod.FunctionType(List(watpe.RefType.anyref), Nil)

instrs.block(anyRefToVoidSig) { isNullLabel =>
// exprNonNull := expr; branch to isNullLabel if it is null
Expand Down Expand Up @@ -1113,8 +1112,8 @@ class ClassEmitter(coreSpec: CoreSpec) {
/** Generates the function import for a top-level export setter. */
private def genTopLevelExportSetter(exportedName: String)(implicit ctx: WasmContext): Unit = {
val functionName = genFunctionName.forTopLevelExportSetter(exportedName)
val functionSig = wamod.FunctionSignature(List(watpe.RefType.anyref), Nil)
val functionType = ctx.moduleBuilder.signatureToTypeName(functionSig)
val functionSig = wamod.FunctionType(List(watpe.RefType.anyref), Nil)
val functionType = ctx.moduleBuilder.functionTypeToTypeName(functionSig)

ctx.moduleBuilder.addImport(
wamod.Import(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ object CoreWasmLib {
}

private def genTags()(implicit ctx: WasmContext): Unit = {
val exceptionSig = FunctionSignature(List(RefType.externref), Nil)
val typeName = ctx.moduleBuilder.signatureToTypeName(exceptionSig)
val exceptionSig = FunctionType(List(RefType.externref), Nil)
val typeName = ctx.moduleBuilder.functionTypeToTypeName(exceptionSig)
ctx.moduleBuilder.addImport(
Import(
"__scalaJSHelpers",
Expand Down Expand Up @@ -332,8 +332,8 @@ object CoreWasmLib {
params: List[Type],
results: List[Type]
): Unit = {
val sig = FunctionSignature(params, results)
val typeName = ctx.moduleBuilder.signatureToTypeName(sig)
val sig = FunctionType(params, results)
val typeName = ctx.moduleBuilder.functionTypeToTypeName(sig)
ctx.moduleBuilder.addImport(
Import("__scalaJSHelpers", name.name, ImportDesc.Func(name, typeName))
)
Expand Down Expand Up @@ -1046,7 +1046,7 @@ object CoreWasmLib {
// if dims == 0 then
// return typeData.arrayOf (which is on the stack)
instrs += I32Eqz
instrs.ifThen(FunctionSignature(List(objectVTableType), List(objectVTableType))) {
instrs.ifThen(FunctionType(List(objectVTableType), List(objectVTableType))) {
instrs += Return
}

Expand Down Expand Up @@ -1785,7 +1785,7 @@ object CoreWasmLib {

// componentTypeData := ref_as_non_null(arrayTypeData.componentType)
// switch (componentTypeData.kind)
val switchClauseSig = FunctionSignature(
val switchClauseSig = FunctionType(
List(arrayTypeDataType, itablesType, Int32),
List(nonNullObjectType)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.scalajs.linker.backend.webassembly._
import org.scalajs.linker.backend.webassembly.{Instructions => wa}
import org.scalajs.linker.backend.webassembly.{Names => wanme}
import org.scalajs.linker.backend.webassembly.{Types => watpe}
import org.scalajs.linker.backend.webassembly.Modules.{FunctionSignature => Sig}
import org.scalajs.linker.backend.webassembly.Modules.{FunctionType => Sig}

import EmbeddedConstants._
import SWasmGen._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ final class WasmContext {
private var _itablesLength: Int = 0
def itablesLength = _itablesLength

private val functionTypes = LinkedHashMap.empty[wamod.FunctionSignature, wanme.TypeName]
private val functionTypes = LinkedHashMap.empty[wamod.FunctionType, wanme.TypeName]
private val tableFunctionTypes = mutable.HashMap.empty[MethodName, wanme.TypeName]
private val constantStringGlobals = LinkedHashMap.empty[String, StringData]
private val classItableGlobals = mutable.ListBuffer.empty[ClassName]
private val closureDataTypes = LinkedHashMap.empty[List[Type], wanme.TypeName]
private val reflectiveProxies = LinkedHashMap.empty[MethodName, Int]

val moduleBuilder: ModuleBuilder = {
new ModuleBuilder(new ModuleBuilder.FunctionSignatureProvider {
def signatureToTypeName(sig: wamod.FunctionSignature): wanme.TypeName = {
new ModuleBuilder(new ModuleBuilder.FunctionTypeProvider {
def functionTypeToTypeName(sig: wamod.FunctionType): wanme.TypeName = {
functionTypes.getOrElseUpdate(
sig, {
val typeName = genTypeName.forFunction(functionTypes.size)
moduleBuilder.addRecType(typeName, wamod.FunctionType(sig))
moduleBuilder.addRecType(typeName, sig)
typeName
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ final class FunctionBuilder(

// Helpers to build structured control flow

def sigToBlockType(sig: FunctionSignature): BlockType = sig match {
case FunctionSignature(Nil, Nil) =>
def sigToBlockType(sig: FunctionType): BlockType = sig match {
case FunctionType(Nil, Nil) =>
BlockType.ValueType()
case FunctionSignature(Nil, resultType :: Nil) =>
case FunctionType(Nil, resultType :: Nil) =>
BlockType.ValueType(resultType)
case _ =>
BlockType.FunctionType(moduleBuilder.signatureToTypeName(sig))
BlockType.FunctionType(moduleBuilder.functionTypeToTypeName(sig))
}

def ifThenElse(blockType: BlockType)(thenp: => Unit)(elsep: => Unit): Unit = {
Expand All @@ -94,11 +94,11 @@ final class FunctionBuilder(
def ifThenElse(resultType: Type)(thenp: => Unit)(elsep: => Unit): Unit =
ifThenElse(BlockType.ValueType(resultType))(thenp)(elsep)

def ifThenElse(sig: FunctionSignature)(thenp: => Unit)(elsep: => Unit): Unit =
def ifThenElse(sig: FunctionType)(thenp: => Unit)(elsep: => Unit): Unit =
ifThenElse(sigToBlockType(sig))(thenp)(elsep)

def ifThenElse(resultTypes: List[Type])(thenp: => Unit)(elsep: => Unit): Unit =
ifThenElse(FunctionSignature(Nil, resultTypes))(thenp)(elsep)
ifThenElse(FunctionType(Nil, resultTypes))(thenp)(elsep)

def ifThenElse()(thenp: => Unit)(elsep: => Unit): Unit =
ifThenElse(BlockType.ValueType())(thenp)(elsep)
Expand All @@ -109,11 +109,11 @@ final class FunctionBuilder(
instrs += End
}

def ifThen(sig: FunctionSignature)(thenp: => Unit): Unit =
def ifThen(sig: FunctionType)(thenp: => Unit): Unit =
ifThen(sigToBlockType(sig))(thenp)

def ifThen(resultTypes: List[Type])(thenp: => Unit): Unit =
ifThen(FunctionSignature(Nil, resultTypes))(thenp)
ifThen(FunctionType(Nil, resultTypes))(thenp)

def ifThen()(thenp: => Unit): Unit =
ifThen(BlockType.ValueType())(thenp)
Expand All @@ -132,11 +132,11 @@ final class FunctionBuilder(
def block[A]()(body: LabelName => A): A =
block(BlockType.ValueType())(body)

def block[A](sig: FunctionSignature)(body: LabelName => A): A =
def block[A](sig: FunctionType)(body: LabelName => A): A =
block(sigToBlockType(sig))(body)

def block[A](resultTypes: List[Type])(body: LabelName => A): A =
block(FunctionSignature(Nil, resultTypes))(body)
block(FunctionType(Nil, resultTypes))(body)

def loop[A](blockType: BlockType)(body: LabelName => A): A = {
val label = genLabel()
Expand All @@ -152,11 +152,11 @@ final class FunctionBuilder(
def loop[A]()(body: LabelName => A): A =
loop(BlockType.ValueType())(body)

def loop[A](sig: FunctionSignature)(body: LabelName => A): A =
def loop[A](sig: FunctionType)(body: LabelName => A): A =
loop(sigToBlockType(sig))(body)

def loop[A](resultTypes: List[Type])(body: LabelName => A): A =
loop(FunctionSignature(Nil, resultTypes))(body)
loop(FunctionType(Nil, resultTypes))(body)

def whileLoop()(cond: => Unit)(body: => Unit): Unit = {
loop() { loopLabel =>
Expand All @@ -181,11 +181,11 @@ final class FunctionBuilder(
def tryTable[A]()(clauses: List[CatchClause])(body: => A): A =
tryTable(BlockType.ValueType())(clauses)(body)

def tryTable[A](sig: FunctionSignature)(clauses: List[CatchClause])(body: => A): A =
def tryTable[A](sig: FunctionType)(clauses: List[CatchClause])(body: => A): A =
tryTable(sigToBlockType(sig))(clauses)(body)

def tryTable[A](resultTypes: List[Type])(clauses: List[CatchClause])(body: => A): A =
tryTable(FunctionSignature(Nil, resultTypes))(clauses)(body)
tryTable(FunctionType(Nil, resultTypes))(clauses)(body)

/** Builds a `switch` over a scrutinee using a `br_table` instruction.
*
Expand All @@ -212,8 +212,8 @@ final class FunctionBuilder(
* must consume at least all the results of the scrutinee.
*/
def switch(
scrutineeSig: FunctionSignature,
clauseSig: FunctionSignature
scrutineeSig: FunctionType,
clauseSig: FunctionType
)(scrutinee: () => Unit)(clauses: (List[Int], () => Unit)*)(default: () => Unit): Unit = {
val clauseLabels = clauses.map(_ => genLabel())

Expand All @@ -239,11 +239,11 @@ final class FunctionBuilder(
)
val (doneBlockType, clauseBlockType) = {
val clauseParamsComingFromAbove = clauseSig.params.drop(scrutineeSig.results.size)
val doneBlockSig = FunctionSignature(
val doneBlockSig = FunctionType(
clauseParamsComingFromAbove ::: scrutineeSig.params,
clauseSig.results
)
val clauseBlockSig = FunctionSignature(
val clauseBlockSig = FunctionType(
clauseParamsComingFromAbove ::: scrutineeSig.params,
clauseSig.params
)
Expand Down Expand Up @@ -277,29 +277,29 @@ final class FunctionBuilder(
}

def switch(
clauseSig: FunctionSignature
clauseSig: FunctionType
)(scrutinee: () => Unit)(clauses: (List[Int], () => Unit)*)(default: () => Unit): Unit = {
switch(FunctionSignature.NilToNil, clauseSig)(scrutinee)(clauses: _*)(default)
switch(FunctionType.NilToNil, clauseSig)(scrutinee)(clauses: _*)(default)
}

def switch(
resultType: Type
)(scrutinee: () => Unit)(clauses: (List[Int], () => Unit)*)(default: () => Unit): Unit = {
switch(FunctionSignature(Nil, List(resultType)))(scrutinee)(clauses: _*)(default)
switch(FunctionType(Nil, List(resultType)))(scrutinee)(clauses: _*)(default)
}

def switch()(
scrutinee: () => Unit
)(clauses: (List[Int], () => Unit)*)(default: () => Unit): Unit = {
switch(FunctionSignature.NilToNil)(scrutinee)(clauses: _*)(default)
switch(FunctionType.NilToNil)(scrutinee)(clauses: _*)(default)
}

// Final result

def buildAndAddToModule(): Function = {
val functionTypeName = specialFunctionType.getOrElse {
val sig = FunctionSignature(params.toList.map(_.typ), resultTypes)
moduleBuilder.signatureToTypeName(sig)
val sig = FunctionType(params.toList.map(_.typ), resultTypes)
moduleBuilder.functionTypeToTypeName(sig)
}

val dcedInstrs = localDeadCodeEliminationOfInstrs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scala.collection.mutable
import Names._
import Modules._

final class ModuleBuilder(functionSignatureProvider: ModuleBuilder.FunctionSignatureProvider) {
final class ModuleBuilder(functionSignatureProvider: ModuleBuilder.FunctionTypeProvider) {
import ModuleBuilder._

/** Items are `RecType | RecTypeBuilder`. */
Expand All @@ -20,8 +20,8 @@ final class ModuleBuilder(functionSignatureProvider: ModuleBuilder.FunctionSigna
private val elems: mutable.ListBuffer[Element] = new mutable.ListBuffer()
private val datas: mutable.ListBuffer[Data] = new mutable.ListBuffer()

def signatureToTypeName(sig: FunctionSignature): TypeName =
functionSignatureProvider.signatureToTypeName(sig)
def functionTypeToTypeName(sig: FunctionType): TypeName =
functionSignatureProvider.functionTypeToTypeName(sig)

def addRecType(typ: RecType): Unit = types += typ
def addRecType(typ: SubType): Unit = addRecType(RecType(typ))
Expand Down Expand Up @@ -64,8 +64,8 @@ final class ModuleBuilder(functionSignatureProvider: ModuleBuilder.FunctionSigna
}

object ModuleBuilder {
trait FunctionSignatureProvider {
def signatureToTypeName(sig: FunctionSignature): TypeName
trait FunctionTypeProvider {
def functionTypeToTypeName(sig: FunctionType): TypeName
}

final class RecTypeBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,10 @@ object Modules {

sealed abstract class CompositeType

final case class FunctionSignature(
params: List[Type],
results: List[Type]
)
object FunctionSignature {
val NilToNil: FunctionSignature = FunctionSignature(Nil, Nil)
}
final case class FunctionType(params: List[Type], results: List[Type]) extends CompositeType

final case class FunctionType(
params: List[Type],
results: List[Type]
) extends CompositeType
object FunctionType {
def apply(sig: FunctionSignature): FunctionType = {
FunctionType(sig.params, sig.results)
}
val NilToNil: FunctionType = FunctionType(Nil, Nil)
}

final case class StructType(fields: List[StructField]) extends CompositeType
Expand Down

0 comments on commit c5e4d17

Please # to comment.