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

Commit 520245c

Browse files
committed
Use namespacing conventions in Emitter.
The choice of what to import with or without namespace is the same as the `Emitter` in the JS backend. IR trees are only required in `genStartFunction`, so we confine their imports there. Arguably `genStartFunction` conceptually belongs more to `ClassEmitter` than `Emitter`.
1 parent b1933bb commit 520245c

1 file changed

Lines changed: 50 additions & 47 deletions

File tree

  • wasm/src/main/scala/org/scalajs/linker/backend/wasmemitter

wasm/src/main/scala/org/scalajs/linker/backend/wasmemitter/Emitter.scala

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package org.scalajs.linker.backend.wasmemitter
22

33
import org.scalajs.ir.Names._
4-
import org.scalajs.ir.{Types => IRTypes}
5-
import org.scalajs.ir.{Trees => IRTrees}
4+
import org.scalajs.ir.Types._
65
import org.scalajs.ir.Position
76

87
import org.scalajs.linker.interface._
98
import org.scalajs.linker.interface.unstable._
109
import org.scalajs.linker.standard._
1110
import org.scalajs.linker.standard.ModuleSet.ModuleID
1211

13-
import org.scalajs.linker.backend.webassembly._
14-
import org.scalajs.linker.backend.webassembly.Instructions._
15-
import org.scalajs.linker.backend.webassembly.Modules._
16-
import org.scalajs.linker.backend.webassembly.Types._
12+
import org.scalajs.linker.backend.webassembly.FunctionBuilder
13+
import org.scalajs.linker.backend.webassembly.{Instructions => wa}
14+
import org.scalajs.linker.backend.webassembly.{Modules => wamod}
15+
import org.scalajs.linker.backend.webassembly.{Types => watpe}
1716

1817
import org.scalajs.logging.Logger
1918

@@ -91,15 +90,17 @@ final class Emitter(config: Emitter.Config) {
9190

9291
// string
9392
val (stringPool, stringPoolCount) = ctx.getFinalStringPool()
94-
ctx.moduleBuilder.addData(WasmData(genDataName.string, stringPool, WasmData.Mode.Passive))
93+
ctx.moduleBuilder.addData(
94+
wamod.WasmData(genDataName.string, stringPool, wamod.WasmData.Mode.Passive)
95+
)
9596
ctx.addGlobal(
96-
WasmGlobal(
97+
wamod.WasmGlobal(
9798
genGlobalName.stringLiteralCache,
98-
WasmRefType(genTypeName.anyArray),
99-
WasmExpr(
99+
watpe.WasmRefType(genTypeName.anyArray),
100+
wamod.WasmExpr(
100101
List(
101-
I32_CONST(stringPoolCount),
102-
ARRAY_NEW_DEFAULT(genTypeName.anyArray)
102+
wa.I32_CONST(stringPoolCount),
103+
wa.ARRAY_NEW_DEFAULT(genTypeName.anyArray)
103104
)
104105
),
105106
isMutable = false
@@ -115,6 +116,8 @@ final class Emitter(config: Emitter.Config) {
115116
classesWithStaticInit: List[ClassName],
116117
topLevelExportDefs: List[LinkedTopLevelExport]
117118
)(implicit ctx: WasmContext): Unit = {
119+
import org.scalajs.ir.Trees._
120+
118121
implicit val pos = Position.NoPosition
119122

120123
val fb = new FunctionBuilder(ctx.moduleBuilder, genFunctionName.start, pos)
@@ -128,13 +131,13 @@ final class Emitter(config: Emitter.Config) {
128131

129132
interfaces.foreach { iface =>
130133
val idx = ctx.getItableIdx(iface)
131-
instrs += GLOBAL_GET(genGlobalName.forITable(className))
132-
instrs += I32_CONST(idx)
134+
instrs += wa.GLOBAL_GET(genGlobalName.forITable(className))
135+
instrs += wa.I32_CONST(idx)
133136

134137
for (method <- iface.tableEntries)
135138
instrs += ctx.refFuncWithDeclaration(resolvedMethodInfos(method).tableEntryName)
136-
instrs += STRUCT_NEW(genTypeName.forITable(iface.name))
137-
instrs += ARRAY_SET(genTypeName.itables)
139+
instrs += wa.STRUCT_NEW(genTypeName.forITable(iface.name))
140+
instrs += wa.ARRAY_SET(genTypeName.itables)
138141
}
139142
}
140143

@@ -148,53 +151,53 @@ final class Emitter(config: Emitter.Config) {
148151
// Use getClassInfoOption in case the reachability analysis got rid of those interfaces
149152
interfaceInfo <- ctx.getClassInfoOption(interfaceName)
150153
} {
151-
instrs += GLOBAL_GET(globalName)
152-
instrs += I32_CONST(ctx.getItableIdx(interfaceInfo))
154+
instrs += wa.GLOBAL_GET(globalName)
155+
instrs += wa.I32_CONST(ctx.getItableIdx(interfaceInfo))
153156

154157
for (method <- interfaceInfo.tableEntries)
155158
instrs += ctx.refFuncWithDeclaration(resolvedMethodInfos(method).tableEntryName)
156-
instrs += STRUCT_NEW(genTypeName.forITable(interfaceName))
157-
instrs += ARRAY_SET(genTypeName.itables)
159+
instrs += wa.STRUCT_NEW(genTypeName.forITable(interfaceName))
160+
instrs += wa.ARRAY_SET(genTypeName.itables)
158161
}
159162
}
160163

161164
// Initialize the JS private field symbols
162165

163166
for (fieldName <- ctx.getAllJSPrivateFieldNames()) {
164-
instrs += CALL(genFunctionName.newSymbol)
165-
instrs += GLOBAL_SET(genGlobalName.forJSPrivateField(fieldName))
167+
instrs += wa.CALL(genFunctionName.newSymbol)
168+
instrs += wa.GLOBAL_SET(genGlobalName.forJSPrivateField(fieldName))
166169
}
167170

168171
// Emit the static initializers
169172

170173
for (className <- classesWithStaticInit) {
171174
val funcName = genFunctionName.forMethod(
172-
IRTrees.MemberNamespace.StaticConstructor,
175+
MemberNamespace.StaticConstructor,
173176
className,
174177
StaticInitializerName
175178
)
176-
instrs += CALL(funcName)
179+
instrs += wa.CALL(funcName)
177180
}
178181

179182
// Initialize the top-level exports that require it
180183

181184
for (tle <- topLevelExportDefs) {
182185
tle.tree match {
183-
case IRTrees.TopLevelJSClassExportDef(_, exportName) =>
184-
instrs += CALL(genFunctionName.loadJSClass(tle.owningClass))
185-
instrs += GLOBAL_SET(genGlobalName.forTopLevelExport(tle.exportName))
186-
case IRTrees.TopLevelModuleExportDef(_, exportName) =>
187-
instrs += CALL(genFunctionName.loadModule(tle.owningClass))
188-
instrs += GLOBAL_SET(genGlobalName.forTopLevelExport(tle.exportName))
189-
case IRTrees.TopLevelMethodExportDef(_, methodDef) =>
186+
case TopLevelJSClassExportDef(_, exportName) =>
187+
instrs += wa.CALL(genFunctionName.loadJSClass(tle.owningClass))
188+
instrs += wa.GLOBAL_SET(genGlobalName.forTopLevelExport(tle.exportName))
189+
case TopLevelModuleExportDef(_, exportName) =>
190+
instrs += wa.CALL(genFunctionName.loadModule(tle.owningClass))
191+
instrs += wa.GLOBAL_SET(genGlobalName.forTopLevelExport(tle.exportName))
192+
case TopLevelMethodExportDef(_, methodDef) =>
190193
// We only need initialization if there is a restParam
191194
if (methodDef.restParam.isDefined) {
192195
instrs += ctx.refFuncWithDeclaration(genFunctionName.forExport(tle.exportName))
193-
instrs += I32_CONST(methodDef.args.size)
194-
instrs += CALL(genFunctionName.closureRestNoData)
195-
instrs += GLOBAL_SET(genGlobalName.forTopLevelExport(tle.exportName))
196+
instrs += wa.I32_CONST(methodDef.args.size)
197+
instrs += wa.CALL(genFunctionName.closureRestNoData)
198+
instrs += wa.GLOBAL_SET(genGlobalName.forTopLevelExport(tle.exportName))
196199
}
197-
case IRTrees.TopLevelFieldExportDef(_, _, _) =>
200+
case TopLevelFieldExportDef(_, _, _) =>
198201
// Nothing to do
199202
()
200203
}
@@ -205,27 +208,27 @@ final class Emitter(config: Emitter.Config) {
205208
moduleInitializers.foreach { init =>
206209
def genCallStatic(className: ClassName, methodName: MethodName): Unit = {
207210
val functionName =
208-
genFunctionName.forMethod(IRTrees.MemberNamespace.PublicStatic, className, methodName)
209-
instrs += CALL(functionName)
211+
genFunctionName.forMethod(MemberNamespace.PublicStatic, className, methodName)
212+
instrs += wa.CALL(functionName)
210213
}
211214

212215
ModuleInitializerImpl.fromInitializer(init) match {
213216
case ModuleInitializerImpl.MainMethodWithArgs(className, encodedMainMethodName, args) =>
214217
// vtable of Array[String]
215-
instrs += GLOBAL_GET(genGlobalName.forVTable(BoxedStringClass))
216-
instrs += I32_CONST(1)
217-
instrs += CALL(genFunctionName.arrayTypeData)
218+
instrs += wa.GLOBAL_GET(genGlobalName.forVTable(BoxedStringClass))
219+
instrs += wa.I32_CONST(1)
220+
instrs += wa.CALL(genFunctionName.arrayTypeData)
218221

219222
// itable of Array[String]
220-
instrs += GLOBAL_GET(genGlobalName.arrayClassITable)
223+
instrs += wa.GLOBAL_GET(genGlobalName.arrayClassITable)
221224

222225
// underlying array of args
223226
args.foreach(arg => instrs ++= ctx.getConstantStringInstr(arg))
224-
instrs += ARRAY_NEW_FIXED(genTypeName.anyArray, args.size)
227+
instrs += wa.ARRAY_NEW_FIXED(genTypeName.anyArray, args.size)
225228

226229
// array object
227-
val stringArrayTypeRef = IRTypes.ArrayTypeRef(IRTypes.ClassRef(BoxedStringClass), 1)
228-
instrs += STRUCT_NEW(genTypeName.forArrayClass(stringArrayTypeRef))
230+
val stringArrayTypeRef = ArrayTypeRef(ClassRef(BoxedStringClass), 1)
231+
instrs += wa.STRUCT_NEW(genTypeName.forArrayClass(stringArrayTypeRef))
229232

230233
// call
231234
genCallStatic(className, encodedMainMethodName)
@@ -256,10 +259,10 @@ final class Emitter(config: Emitter.Config) {
256259
* introduce these declarations.
257260
*/
258261
val exprs = funcDeclarations.map { name =>
259-
WasmExpr(List(REF_FUNC(name)))
262+
wamod.WasmExpr(List(wa.REF_FUNC(name)))
260263
}
261264
ctx.moduleBuilder.addElement(
262-
WasmElement(WasmRefType.funcref, exprs, WasmElement.Mode.Declarative)
265+
wamod.WasmElement(watpe.WasmRefType.funcref, exprs, wamod.WasmElement.Mode.Declarative)
263266
)
264267
}
265268
}
@@ -315,7 +318,7 @@ object Emitter {
315318
}
316319

317320
final class Result(
318-
val wasmModule: WasmModule,
321+
val wasmModule: wamod.WasmModule,
319322
val loaderContent: Array[Byte],
320323
val jsFileContent: String
321324
)

0 commit comments

Comments
 (0)