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

Commit 0117e8a

Browse files
committed
Directly emit Wasm instructions for module initializers.
Before, we created synthetic IR trees that we sent to the expression builder. Now, we instead directly emit the appropriate Wasm instructions. This adds a bit of duplication of logic on how arrays are built, but it removes the direct dependency of the `Emitter` on `WasmExpressionBuilder`.
1 parent c24131d commit 0117e8a

File tree

1 file changed

+20
-17
lines changed
  • wasm/src/main/scala/org/scalajs/linker/backend/wasmemitter

1 file changed

+20
-17
lines changed

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

+20-17
Original file line numberDiff line numberDiff line change
@@ -210,27 +210,30 @@ final class Emitter(config: Emitter.Config) {
210210
instrs += WasmInstr.CALL(functionName)
211211
}
212212

213-
val stringArrayTypeRef = IRTypes.ArrayTypeRef(IRTypes.ClassRef(BoxedStringClass), 1)
214-
215-
val callTree = ModuleInitializerImpl.fromInitializer(init) match {
213+
ModuleInitializerImpl.fromInitializer(init) match {
216214
case ModuleInitializerImpl.MainMethodWithArgs(className, encodedMainMethodName, args) =>
217-
IRTrees.ApplyStatic(
218-
IRTrees.ApplyFlags.empty,
219-
className,
220-
IRTrees.MethodIdent(encodedMainMethodName),
221-
List(IRTrees.ArrayValue(stringArrayTypeRef, args.map(IRTrees.StringLiteral(_))))
222-
)(IRTypes.NoType)
215+
// vtable of Array[String]
216+
instrs += GLOBAL_GET(genGlobalName.forVTable(BoxedStringClass))
217+
instrs += I32_CONST(1)
218+
instrs += CALL(genFunctionName.arrayTypeData)
219+
220+
// itable of Array[String]
221+
instrs += GLOBAL_GET(genGlobalName.arrayClassITable)
222+
223+
// underlying array of args
224+
args.foreach(arg => instrs ++= ctx.getConstantStringInstr(arg))
225+
instrs += ARRAY_NEW_FIXED(genTypeName.anyArray, args.size)
226+
227+
// array object
228+
val stringArrayTypeRef = IRTypes.ArrayTypeRef(IRTypes.ClassRef(BoxedStringClass), 1)
229+
instrs += STRUCT_NEW(genTypeName.forArrayClass(stringArrayTypeRef))
230+
231+
// call
232+
genCallStatic(className, encodedMainMethodName)
223233

224234
case ModuleInitializerImpl.VoidMainMethod(className, encodedMainMethodName) =>
225-
IRTrees.ApplyStatic(
226-
IRTrees.ApplyFlags.empty,
227-
className,
228-
IRTrees.MethodIdent(encodedMainMethodName),
229-
Nil
230-
)(IRTypes.NoType)
235+
genCallStatic(className, encodedMainMethodName)
231236
}
232-
233-
WasmExpressionBuilder.generateIRBody(callTree, IRTypes.NoType)
234237
}
235238

236239
// Finish the start function

0 commit comments

Comments
 (0)