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

Commit

Permalink
Merge pull request #133 from sjrd/cleanup-wasm-model
Browse files Browse the repository at this point in the history
Cleanup our WebAssembly model.
  • Loading branch information
tanishiking authored May 20, 2024
2 parents 93c16a6 + bd84686 commit cb49b3a
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
val global = wamod.Global(
genGlobalName.forStaticField(name.name),
transformType(ftpe),
wamod.Expr(List(genZeroOf(ftpe))),
wa.Expr(List(genZeroOf(ftpe))),
isMutable = true
)
ctx.addGlobal(global)
Expand Down Expand Up @@ -263,7 +263,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
wamod.Global(
genGlobalName.forVTable(className),
watpe.RefType(typeDataTypeName),
wamod.Expr(instrs),
wa.Expr(instrs),
isMutable = false
)
)
Expand Down Expand Up @@ -301,27 +301,27 @@ class ClassEmitter(coreSpec: CoreSpec) {
}

// Declare the struct type for the class
val vtableField = wamod.StructField(
val vtableField = watpe.StructField(
genFieldName.objStruct.vtable,
watpe.RefType(vtableTypeName),
isMutable = false
)
val itablesField = wamod.StructField(
val itablesField = watpe.StructField(
genFieldName.objStruct.itables,
watpe.RefType.nullable(genTypeName.itables),
isMutable = false
)
val fields = classInfo.allFieldDefs.map { field =>
wamod.StructField(
watpe.StructField(
genFieldName.forClassInstanceField(field.name.name),
transformType(field.ftpe),
isMutable = true // initialized by the constructors, so always mutable at the Wasm level
)
}
val structTypeName = genTypeName.forClass(clazz.name.name)
val superType = clazz.superClass.map(s => genTypeName.forClass(s.name))
val structType = wamod.StructType(vtableField :: itablesField :: fields)
val subType = wamod.SubType(structTypeName, isFinal = false, superType, structType)
val structType = watpe.StructType(vtableField :: itablesField :: fields)
val subType = watpe.SubType(structTypeName, isFinal = false, superType, structType)
ctx.mainRecType.addSubType(subType)

// Define the `new` function and possibly the `clone` function, unless the class is abstract
Expand All @@ -340,7 +340,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
val global = wamod.Global(
genGlobalName.forModuleInstance(clazz.name.name),
watpe.RefType.nullable(heapType),
wamod.Expr(List(wa.RefNull(heapType))),
wa.Expr(List(wa.RefNull(heapType))),
isMutable = true
)
ctx.addGlobal(global)
Expand All @@ -355,7 +355,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
val typeName = genTypeName.forVTable(classInfo.name)
val vtableFields =
classInfo.tableEntries.map { methodName =>
wamod.StructField(
watpe.StructField(
genFieldName.forMethodTableEntry(methodName),
watpe.RefType(ctx.tableFunctionType(methodName)),
isMutable = false
Expand All @@ -365,8 +365,8 @@ class ClassEmitter(coreSpec: CoreSpec) {
case None => genTypeName.typeData
case Some(s) => genTypeName.forVTable(s)
}
val structType = wamod.StructType(CoreWasmLib.typeDataStructFields ::: vtableFields)
val subType = wamod.SubType(typeName, isFinal = false, Some(superType), structType)
val structType = watpe.StructType(CoreWasmLib.typeDataStructFields ::: vtableFields)
val subType = watpe.SubType(typeName, isFinal = false, Some(superType), structType)
ctx.mainRecType.addSubType(subType)
typeName
}
Expand Down Expand Up @@ -435,7 +435,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
* If `expr` is `undefined`, it would be `(1 << 4) == 0b00010000`, which
* would give `false`.
*/
val anyRefToVoidSig = wamod.FunctionType(List(watpe.RefType.anyref), Nil)
val anyRefToVoidSig = watpe.FunctionType(List(watpe.RefType.anyref), Nil)

instrs.block(anyRefToVoidSig) { isNullLabel =>
// exprNonNull := expr; branch to isNullLabel if it is null
Expand Down Expand Up @@ -596,7 +596,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
val global = wamod.Global(
globalName,
watpe.RefType(genTypeName.itables),
wamod.Expr(itablesInit),
wa.Expr(itablesInit),
isMutable = false
)
ctx.addGlobalITable(clazz.className, global)
Expand All @@ -609,9 +609,9 @@ class ClassEmitter(coreSpec: CoreSpec) {
val className = clazz.name.name
val classInfo = ctx.getClassInfo(clazz.className)
val itableTypeName = genTypeName.forITable(className)
val itableType = wamod.StructType(
val itableType = watpe.StructType(
classInfo.tableEntries.map { methodName =>
wamod.StructField(
watpe.StructField(
genFieldName.forMethodTableEntry(methodName),
watpe.RefType(ctx.tableFunctionType(methodName)),
isMutable = false
Expand All @@ -635,7 +635,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
wamod.Global(
genGlobalName.forJSPrivateField(name.name),
watpe.RefType.anyref,
wamod.Expr(List(wa.RefNull(watpe.HeapType.Any))),
wa.Expr(List(wa.RefNull(watpe.HeapType.Any))),
isMutable = true
)
)
Expand Down Expand Up @@ -946,7 +946,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
val cachedJSClassGlobal = wamod.Global(
genGlobalName.forJSClassValue(clazz.className),
watpe.RefType.anyref,
wamod.Expr(List(wa.RefNull(watpe.HeapType.Any))),
wa.Expr(List(wa.RefNull(watpe.HeapType.Any))),
isMutable = true
)
ctx.addGlobal(cachedJSClassGlobal)
Expand Down Expand Up @@ -979,7 +979,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
wamod.Global(
cacheGlobalName,
watpe.RefType.anyref,
wamod.Expr(List(wa.RefNull(watpe.HeapType.Any))),
wa.Expr(List(wa.RefNull(watpe.HeapType.Any))),
isMutable = true
)
)
Expand Down Expand Up @@ -1014,7 +1014,7 @@ 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.FunctionType(List(watpe.RefType.anyref), Nil)
val functionSig = watpe.FunctionType(List(watpe.RefType.anyref), Nil)
val functionType = ctx.moduleBuilder.functionTypeToTypeName(functionSig)

ctx.moduleBuilder.addImport(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ final class Emitter(config: Emitter.Config) {
wamod.Global(
genGlobalName.stringLiteralCache,
watpe.RefType(genTypeName.anyArray),
wamod.Expr(
wa.Expr(
List(
wa.I32Const(stringPoolCount),
wa.ArrayNewDefault(genTypeName.anyArray)
Expand Down Expand Up @@ -265,7 +265,7 @@ final class Emitter(config: Emitter.Config) {
* introduce these declarations.
*/
val exprs = funcDeclarations.map { name =>
wamod.Expr(List(wa.RefFunc(name)))
wa.Expr(List(wa.RefFunc(name)))
}
ctx.moduleBuilder.addElement(
wamod.Element(watpe.RefType.funcref, exprs, wamod.Element.Mode.Declarative)
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.{FunctionType => Sig}
import org.scalajs.linker.backend.webassembly.Types.{FunctionType => Sig}

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

private val functionTypes = LinkedHashMap.empty[wamod.FunctionType, wanme.TypeName]
private val functionTypes = LinkedHashMap.empty[watpe.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]
Expand All @@ -40,7 +40,7 @@ final class WasmContext {

val moduleBuilder: ModuleBuilder = {
new ModuleBuilder(new ModuleBuilder.FunctionTypeProvider {
def functionTypeToTypeName(sig: wamod.FunctionType): wanme.TypeName = {
def functionTypeToTypeName(sig: watpe.FunctionType): wanme.TypeName = {
functionTypes.getOrElseUpdate(
sig, {
val typeName = genTypeName.forFunction(functionTypes.size)
Expand Down Expand Up @@ -133,7 +133,7 @@ final class WasmContext {
)
mainRecType.addSubType(
typeName,
wamod.FunctionType(watpe.RefType.any :: regularParamTyps, resultTyp)
watpe.FunctionType(watpe.RefType.any :: regularParamTyps, resultTyp)
)
typeName
}
Expand Down Expand Up @@ -178,16 +178,16 @@ final class WasmContext {
def getClosureDataStructType(captureParamTypes: List[Type]): wanme.TypeName = {
closureDataTypes.getOrElseUpdate(
captureParamTypes, {
val fields: List[wamod.StructField] =
val fields: List[watpe.StructField] =
for ((tpe, i) <- captureParamTypes.zipWithIndex)
yield wamod.StructField(
yield watpe.StructField(
genFieldName.captureParam(i),
TypeTransformer.transformType(tpe)(this),
isMutable = false
)
val structTypeName = genTypeName.captureData(nextClosureDataTypeIndex)
nextClosureDataTypeIndex += 1
val structType = wamod.StructType(fields)
val structType = watpe.StructType(fields)
moduleBuilder.addRecType(structTypeName, structType)
structTypeName
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ class BinaryWriter(module: Module, emitDebugInfo: Boolean) {

private def writeExportSection(buf: Buffer): Unit = {
buf.vec(module.exports) { exp =>
buf.name(exp.exportName)
exp match {
case Export.Function(_, funcName) =>
buf.name(exp.name)
exp.desc match {
case ExportDesc.Func(funcName) =>
buf.byte(0x00)
writeFuncIdx(buf, funcName)
case Export.Global(_, globalName) =>
case ExportDesc.Global(globalName) =>
buf.byte(0x03)
writeGlobalIdx(buf, globalName)
}
Expand Down 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 @@ -7,7 +7,7 @@ import org.scalajs.ir.Position
import Instructions._
import Names._
import Modules._
import Types.Type
import Types._

final class FunctionBuilder(
moduleBuilder: ModuleBuilder,
Expand Down 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 @@ -11,6 +11,11 @@ import Types._
* [[https://webassembly.github.io/spec/core/syntax/instructions.html]]
*/
object Instructions {

/** A WebAssembly `expr`. */
sealed case class Expr(instr: List[Instr])

/** A WebAssembly `instr`. */
sealed abstract class Instr(val mnemonic: String, val opcode: Int)

// Semantic categories of instructions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import scala.collection.mutable

import Names._
import Modules._
import Types._

final class ModuleBuilder(functionSignatureProvider: ModuleBuilder.FunctionTypeProvider) {
import ModuleBuilder._
Expand Down
Loading

0 comments on commit cb49b3a

Please sign in to comment.