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

Commit e198109

Browse files
authored
Merge pull request #11 from sjrd/flat-codegen-with-adapt
Flat codegen with infrastructure for adaptions.
2 parents b3701b9 + ddcfa62 commit e198109

File tree

4 files changed

+389
-314
lines changed

4 files changed

+389
-314
lines changed

wasm/src/main/scala/ir2wasm/TypeTransformer.scala

+6-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ object TypeTransformer {
3535
t: IRTypes.Type
3636
)(implicit ctx: ReadOnlyWasmContext): List[Types.WasmType] =
3737
t match {
38-
case IRTypes.NoType => Nil
39-
case IRTypes.ClassType(className) if className == IRNames.BoxedUnitClass => Nil
40-
case _ => List(transformType(t))
38+
case IRTypes.NoType => Nil
39+
case _ => List(transformType(t))
4140
}
4241
def transformType(t: IRTypes.Type)(implicit ctx: ReadOnlyWasmContext): Types.WasmType =
4342
t match {
@@ -64,6 +63,10 @@ object TypeTransformer {
6463
Types.WasmRefNullType(
6564
Types.WasmHeapType.Type(Names.WasmTypeName.WasmStructTypeName.string)
6665
)
66+
case IRNames.BoxedUnitClass =>
67+
Types.WasmRefNullType(
68+
Types.WasmHeapType.Type(Names.WasmTypeName.WasmStructTypeName.undef)
69+
)
6770
case _ =>
6871
if (ctx.getClassInfo(clazz.className).isInterface)
6972
Types.WasmRefNullType(Types.WasmHeapType.ObjectType)

wasm/src/main/scala/ir2wasm/WasmBuilder.scala

+3-16
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class WasmBuilder {
3434

3535
def transformTopLevelExport(export: LinkedTopLevelExport)(implicit ctx: WasmContext): Unit = {
3636
implicit val fctx = WasmFunctionContext()
37-
val expressionBuilder = new WasmExpressionBuilder(ctx, fctx)
3837
export.tree match {
3938
case d: IRTrees.TopLevelFieldExportDef => ???
4039
case d: IRTrees.TopLevelJSClassExportDef => ???
@@ -295,7 +294,6 @@ class WasmBuilder {
295294
private def transformToplevelMethodExportDef(
296295
exportDef: IRTrees.TopLevelMethodExportDef
297296
)(implicit ctx: WasmContext, fctx: WasmFunctionContext) = {
298-
val builder = new WasmExpressionBuilder(ctx, fctx)
299297
val method = exportDef.methodDef
300298
val methodName = method.name match {
301299
case lit: IRTrees.StringLiteral => lit
@@ -363,15 +361,12 @@ class WasmBuilder {
363361
}
364362
params.foreach(fctx.locals.define)
365363

366-
val instrs = newBody match {
367-
case t: IRTrees.Block => t.stats.flatMap(builder.transformTree)
368-
case _ => builder.transformTree(newBody)
369-
}
364+
val expr = WasmExpressionBuilder.transformBody(newBody, resultType)
370365
val func = WasmFunction(
371366
Names.WasmFunctionName(methodName),
372367
functionType,
373368
fctx.locals.all,
374-
WasmExpr(instrs)
369+
expr
375370
)
376371
ctx.addFunction(func)
377372

@@ -417,18 +412,10 @@ class WasmBuilder {
417412
}).foreach(fctx.locals.define)
418413

419414
// build function body
420-
val builder = new WasmExpressionBuilder(ctx, fctx)
421415
val body = method.body.getOrElse(throw new Exception("abstract method cannot be transformed"))
422416
// val prefix =
423417
// if (method.flags.namespace.isConstructor) builder.objectCreationPrefix(clazz, method) else Nil
424-
val instrs = body match {
425-
case t: IRTrees.Block => t.stats.flatMap(builder.transformTree)
426-
case _ => builder.transformTree(body)
427-
}
428-
val expr = method.resultType match {
429-
case IRTypes.NoType => WasmExpr(instrs)
430-
case _ => WasmExpr(instrs :+ RETURN)
431-
}
418+
val expr = WasmExpressionBuilder.transformBody(body, method.resultType)
432419

433420
val func = WasmFunction(
434421
Names.WasmFunctionName(clazz.name.name, method.name.name),

0 commit comments

Comments
 (0)