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

Commit 94628e7

Browse files
committed
Remove the remaining encoding decisions from Types.
Use `VarGen.genTypeName` instead.
1 parent 390bf19 commit 94628e7

File tree

7 files changed

+45
-53
lines changed

7 files changed

+45
-53
lines changed

Diff for: wasm/src/main/scala/ir2wasm/HelperFunctions.scala

+21-21
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,14 @@ object HelperFunctions {
295295
val fctx = WasmFunctionContext(
296296
genFunctionName.createClassOf,
297297
List("typeData" -> typeDataType),
298-
List(WasmRefType(WasmHeapType.ClassType))
298+
List(WasmRefType(genTypeName.ClassStruct))
299299
)
300300

301301
val List(typeDataParam) = fctx.paramIndices
302302

303303
import fctx.instrs
304304

305-
val classInstanceLocal = fctx.addLocal("classInstance", WasmRefType(WasmHeapType.ClassType))
305+
val classInstanceLocal = fctx.addLocal("classInstance", WasmRefType(genTypeName.ClassStruct))
306306

307307
// classInstance := newDefault$java.lang.Class()
308308
// leave it on the stack for the constructor call
@@ -413,14 +413,14 @@ object HelperFunctions {
413413
val fctx = WasmFunctionContext(
414414
genFunctionName.getClassOf,
415415
List("typeData" -> typeDataType),
416-
List(WasmRefType(WasmHeapType.ClassType))
416+
List(WasmRefType(genTypeName.ClassStruct))
417417
)
418418

419419
val List(typeDataParam) = fctx.paramIndices
420420

421421
import fctx.instrs
422422

423-
instrs.block(WasmRefType(WasmHeapType.ClassType)) { alreadyInitializedLabel =>
423+
instrs.block(WasmRefType(genTypeName.ClassStruct)) { alreadyInitializedLabel =>
424424
// fast path
425425
instrs += LOCAL_GET(typeDataParam)
426426
instrs += STRUCT_GET(genTypeName.typeData, genFieldIdx.typeData.classOfIdx)
@@ -976,7 +976,7 @@ object HelperFunctions {
976976
val fctx = WasmFunctionContext(
977977
genFunctionName.getComponentType,
978978
List("typeData" -> typeDataType),
979-
List(WasmRefType.nullable(WasmHeapType.ClassType))
979+
List(WasmRefType.nullable(genTypeName.ClassStruct))
980980
)
981981

982982
val List(typeDataParam) = fctx.paramIndices
@@ -997,7 +997,7 @@ object HelperFunctions {
997997
instrs += CALL(genFunctionName.getClassOf)
998998
instrs += RETURN
999999
} // end block nullResultLabel
1000-
instrs += REF_NULL(WasmHeapType.ClassType)
1000+
instrs += REF_NULL(WasmHeapType(genTypeName.ClassStruct))
10011001

10021002
fctx.buildAndAddToContext()
10031003
}
@@ -1013,7 +1013,7 @@ object HelperFunctions {
10131013
val fctx = WasmFunctionContext(
10141014
genFunctionName.newArrayOfThisClass,
10151015
List("typeData" -> typeDataType, "lengths" -> WasmRefType.anyref),
1016-
List(WasmRefType(WasmHeapType.ObjectType))
1016+
List(WasmRefType(genTypeName.ObjectStruct))
10171017
)
10181018

10191019
val List(typeDataParam, lengthsParam) = fctx.paramIndices
@@ -1090,7 +1090,7 @@ object HelperFunctions {
10901090
val fctx = WasmFunctionContext(
10911091
genFunctionName.anyGetClass,
10921092
List("value" -> WasmRefType.any),
1093-
List(WasmRefType.nullable(WasmHeapType.ClassType))
1093+
List(WasmRefType.nullable(genTypeName.ClassStruct))
10941094
)
10951095

10961096
val List(valueParam) = fctx.paramIndices
@@ -1100,20 +1100,20 @@ object HelperFunctions {
11001100
val typeDataLocal = fctx.addLocal("typeData", typeDataType)
11011101
val doubleValueLocal = fctx.addLocal("doubleValue", WasmFloat64)
11021102
val intValueLocal = fctx.addLocal("intValue", WasmInt32)
1103-
val ourObjectLocal = fctx.addLocal("ourObject", WasmRefType(WasmHeapType.ObjectType))
1103+
val ourObjectLocal = fctx.addLocal("ourObject", WasmRefType(genTypeName.ObjectStruct))
11041104

11051105
def getHijackedClassTypeDataInstr(className: IRNames.ClassName): WasmInstr =
11061106
GLOBAL_GET(genGlobalName.forVTable(className))
11071107

1108-
instrs.block(WasmRefType.nullable(WasmHeapType.ClassType)) { nonNullClassOfLabel =>
1108+
instrs.block(WasmRefType.nullable(genTypeName.ClassStruct)) { nonNullClassOfLabel =>
11091109
instrs.block(typeDataType) { gotTypeDataLabel =>
1110-
instrs.block(WasmRefType(WasmHeapType.ObjectType)) { ourObjectLabel =>
1110+
instrs.block(WasmRefType(genTypeName.ObjectStruct)) { ourObjectLabel =>
11111111
// if value is our object, jump to $ourObject
11121112
instrs += LOCAL_GET(valueParam)
11131113
instrs += BR_ON_CAST(
11141114
ourObjectLabel,
11151115
WasmRefType.any,
1116-
WasmRefType(WasmHeapType.ObjectType)
1116+
WasmRefType(genTypeName.ObjectStruct)
11171117
)
11181118

11191119
// switch(jsValueType(value)) { ... }
@@ -1209,7 +1209,7 @@ object HelperFunctions {
12091209
}
12101210
) { () =>
12111211
// case _ (JSValueTypeOther) => return null
1212-
instrs += REF_NULL(WasmHeapType.ClassType)
1212+
instrs += REF_NULL(WasmHeapType(genTypeName.ClassStruct))
12131213
instrs += RETURN
12141214
}
12151215

@@ -1263,7 +1263,7 @@ object HelperFunctions {
12631263
val objectVTableType = WasmRefType(genTypeName.ObjectVTable)
12641264
val arrayTypeDataType = objectVTableType
12651265
val itablesType = WasmRefType.nullable(genTypeName.itables)
1266-
val nonNullObjectType = WasmRefType(WasmHeapType.ObjectType)
1266+
val nonNullObjectType = WasmRefType(genTypeName.ObjectStruct)
12671267
val anyArrayType = WasmRefType(genTypeName.anyArray)
12681268

12691269
val fctx = WasmFunctionContext(
@@ -1478,7 +1478,7 @@ object HelperFunctions {
14781478
instrs += LOCAL_TEE(objNonNullLocal)
14791479

14801480
// If `obj` is one of our objects, skip all the jsValueType tests
1481-
instrs += REF_TEST(WasmRefType(WasmHeapType.ObjectType))
1481+
instrs += REF_TEST(WasmRefType(genTypeName.ObjectStruct))
14821482
instrs += I32_EQZ
14831483
instrs.ifThen() {
14841484
instrs.switch() { () =>
@@ -1690,11 +1690,11 @@ object HelperFunctions {
16901690
instrs += BR_ON_CAST_FAIL(
16911691
testFail,
16921692
WasmRefType.anyref,
1693-
WasmRefType(Types.WasmHeapType.ObjectType)
1693+
WasmRefType(genTypeName.ObjectStruct)
16941694
)
16951695

16961696
// get itables and store
1697-
instrs += STRUCT_GET(Types.WasmHeapType.ObjectType.typ, genFieldIdx.objStruct.itables)
1697+
instrs += STRUCT_GET(genTypeName.ObjectStruct, genFieldIdx.objStruct.itables)
16981698
instrs += LOCAL_SET(itables)
16991699

17001700
// Dummy return value from the block
@@ -1767,8 +1767,8 @@ object HelperFunctions {
17671767
WasmHeapType(genTypeName.forClass(clazz.name.name))
17681768
val fctx = WasmFunctionContext(
17691769
genFunctionName.clone(clazz.name.name),
1770-
List("from" -> WasmRefType(WasmHeapType.ObjectType)),
1771-
List(WasmRefType(WasmHeapType.ObjectType))
1770+
List("from" -> WasmRefType(genTypeName.ObjectStruct)),
1771+
List(WasmRefType(genTypeName.ObjectStruct))
17721772
)
17731773
val List(fromParam) = fctx.paramIndices
17741774
import fctx.instrs
@@ -1811,8 +1811,8 @@ object HelperFunctions {
18111811

18121812
val fctx = WasmFunctionContext(
18131813
genFunctionName.clone(arrayTypeRef.base),
1814-
List("from" -> WasmRefType(WasmHeapType.ObjectType)),
1815-
List(WasmRefType(WasmHeapType.ObjectType))
1814+
List("from" -> WasmRefType(genTypeName.ObjectStruct)),
1815+
List(WasmRefType(genTypeName.ObjectStruct))
18161816
)
18171817
val List(fromParam) = fctx.paramIndices
18181818
import fctx.instrs

Diff for: wasm/src/main/scala/ir2wasm/TypeTransformer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object TypeTransformer {
5757
if (info.isAncestorOfHijackedClass)
5858
Types.WasmRefType.anyref
5959
else if (info.isInterface)
60-
Types.WasmRefType.nullable(Types.WasmHeapType.ObjectType)
60+
Types.WasmRefType.nullable(genTypeName.ObjectStruct)
6161
else
6262
Types.WasmRefType.nullable(genTypeName.forClass(className))
6363
}

Diff for: wasm/src/main/scala/ir2wasm/VarGen.scala

+5
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ object VarGen {
403403
def forClass(name: ClassName): WasmTypeName =
404404
WasmTypeName(s"c.L${name.nameString}")
405405

406+
val ObjectStruct = forClass(ObjectClass)
407+
val ClassStruct = forClass(ClassClass)
408+
val ThrowableStruct = forClass(ThrowableClass)
409+
val JSExceptionStruct = forClass(SpecialNames.JSExceptionClass)
410+
406411
def captureData(index: Int): WasmTypeName =
407412
WasmTypeName(s"captureData.$index")
408413

Diff for: wasm/src/main/scala/ir2wasm/WasmBuilder.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ class WasmBuilder(coreSpec: CoreSpec) {
360360
// name - initially `null`; filled in by the `typeDataName` helper
361361
REF_NULL(WasmHeapType.Any),
362362
// the classOf instance - initially `null`; filled in by the `createClassOf` helper
363-
REF_NULL(WasmHeapType.ClassType),
363+
REF_NULL(WasmHeapType(genTypeName.ClassStruct)),
364364
// arrayOf, the typeData of an array of this type - initially `null`; filled in by the `arrayTypeData` helper
365365
REF_NULL(WasmHeapType(genTypeName.ObjectVTable)),
366366
// clonefFunction - will be invoked from `clone()` method invokaion on the class

Diff for: wasm/src/main/scala/ir2wasm/WasmContext.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ final class WasmContext extends TypeDefinableWasmContext {
308308
val cloneFunctionTypeName: WasmTypeName =
309309
addFunctionTypeInMainRecType(
310310
WasmFunctionSignature(
311-
List(WasmRefType(WasmHeapType.ObjectType)),
312-
List(WasmRefType(WasmHeapType.ObjectType))
311+
List(WasmRefType(genTypeName.ObjectStruct)),
312+
List(WasmRefType(genTypeName.ObjectStruct))
313313
)
314314
)
315315

@@ -335,7 +335,7 @@ final class WasmContext extends TypeDefinableWasmContext {
335335
WasmStructField(strictAncestors, nullable(genTypeName.typeDataArray), isMutable = false),
336336
WasmStructField(componentType, nullable(genTypeName.typeData), isMutable = false),
337337
WasmStructField(name, anyref, isMutable = true),
338-
WasmStructField(classOfValue, nullable(WasmHeapType.ClassType), isMutable = true),
338+
WasmStructField(classOfValue, nullable(genTypeName.ClassStruct), isMutable = true),
339339
WasmStructField(arrayOf, nullable(genTypeName.ObjectVTable), isMutable = true),
340340
WasmStructField(cloneFunction, nullable(cloneFunctionTypeName), isMutable = false),
341341
WasmStructField(

Diff for: wasm/src/main/scala/ir2wasm/WasmExpressionBuilder.scala

+14-19
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ private class WasmExpressionBuilder private (
388388
instrs += BR_ON_CAST_FAIL(
389389
labelNotOurObject,
390390
Types.WasmRefType.any,
391-
Types.WasmRefType(Types.WasmHeapType.ObjectType)
391+
Types.WasmRefType(genTypeName.ObjectStruct)
392392
)
393393
instrs += STRUCT_GET(
394394
genTypeName.forClass(IRNames.ObjectClass),
@@ -430,16 +430,15 @@ private class WasmExpressionBuilder private (
430430
* This is used in the code paths where we have already ruled out `null`
431431
* values and primitive values (that implement hijacked classes).
432432
*/
433-
val heapTypeForDispatch: Types.WasmHeapType = {
433+
val refTypeForDispatch: Types.WasmRefType = {
434434
if (receiverClassInfo.isInterface)
435-
Types.WasmHeapType.ObjectType
435+
Types.WasmRefType(genTypeName.ObjectStruct)
436436
else
437-
Types.WasmHeapType(genTypeName.forClass(receiverClassName))
437+
Types.WasmRefType(genTypeName.forClass(receiverClassName))
438438
}
439439

440440
// A local for a copy of the receiver that we will use to resolve dispatch
441-
val receiverLocalForDispatch =
442-
fctx.addSyntheticLocal(Types.WasmRefType(heapTypeForDispatch))
441+
val receiverLocalForDispatch = fctx.addSyntheticLocal(refTypeForDispatch)
443442

444443
/* Gen loading of the receiver and check that it is non-null.
445444
* After this codegen, the non-null receiver is on the stack.
@@ -535,11 +534,7 @@ private class WasmExpressionBuilder private (
535534
argsLocals
536535
}
537536

538-
instrs += BR_ON_CAST_FAIL(
539-
labelNotOurObject,
540-
Types.WasmRefType.any,
541-
Types.WasmRefType(heapTypeForDispatch)
542-
)
537+
instrs += BR_ON_CAST_FAIL(labelNotOurObject, Types.WasmRefType.any, refTypeForDispatch)
543538
instrs += LOCAL_TEE(receiverLocalForDispatch)
544539
pushArgs(argsLocals)
545540
genTableDispatch(receiverClassInfo, t.method.name, receiverLocalForDispatch)
@@ -835,7 +830,7 @@ private class WasmExpressionBuilder private (
835830
}
836831

837832
private def genClassOfFromTypeData(loadTypeDataInstr: WasmInstr): Unit = {
838-
instrs.block(Types.WasmRefType(Types.WasmHeapType.ClassType)) { nonNullLabel =>
833+
instrs.block(Types.WasmRefType(genTypeName.ClassStruct)) { nonNullLabel =>
839834
// fast path first
840835
instrs += loadTypeDataInstr
841836
instrs += STRUCT_GET(genTypeName.typeData, genFieldIdx.typeData.classOfIdx)
@@ -1249,7 +1244,7 @@ private class WasmExpressionBuilder private (
12491244

12501245
// A local for a copy of the receiver that we will use to resolve dispatch
12511246
val receiverLocalForDispatch =
1252-
fctx.addSyntheticLocal(Types.WasmRefType(Types.WasmHeapType.ObjectType))
1247+
fctx.addSyntheticLocal(Types.WasmRefType(genTypeName.ObjectStruct))
12531248

12541249
val objectClassInfo = ctx.getClassInfo(IRNames.ObjectClass)
12551250

@@ -1309,7 +1304,7 @@ private class WasmExpressionBuilder private (
13091304
instrs += BR_ON_CAST_FAIL(
13101305
labelNotOurObject,
13111306
Types.WasmRefType.anyref,
1312-
Types.WasmRefType(Types.WasmHeapType.ObjectType)
1307+
Types.WasmRefType(genTypeName.ObjectStruct)
13131308
)
13141309
instrs += LOCAL_TEE(receiverLocalForDispatch)
13151310
genTableDispatch(objectClassInfo, toStringMethodName, receiverLocalForDispatch)
@@ -1569,7 +1564,7 @@ private class WasmExpressionBuilder private (
15691564
Types.WasmRefType.nullable(genTypeName.forClass(targetClassName))
15701565
)
15711566
} else if (info.isInterface) {
1572-
instrs += REF_CAST(Types.WasmRefType.nullable(Types.WasmHeapType.ObjectType))
1567+
instrs += REF_CAST(Types.WasmRefType.nullable(genTypeName.ObjectStruct))
15731568
}
15741569

15751570
case IRTypes.ArrayType(arrayTypeRef) =>
@@ -1971,7 +1966,7 @@ private class WasmExpressionBuilder private (
19711966

19721967
private def genWrapAsThrowable(tree: IRTrees.WrapAsThrowable): IRTypes.Type = {
19731968
val throwableClassType = IRTypes.ClassType(IRNames.ThrowableClass)
1974-
val nonNullThrowableTyp = Types.WasmRefType(Types.WasmHeapType.ThrowableType)
1969+
val nonNullThrowableTyp = Types.WasmRefType(genTypeName.ThrowableStruct)
19751970

19761971
val jsExceptionTyp =
19771972
TypeTransformer.transformClassType(SpecialNames.JSExceptionClass)(ctx).toNonNullable
@@ -2021,8 +2016,8 @@ private class WasmExpressionBuilder private (
20212016
// if !expr.isInstanceOf[js.JavaScriptException], then br $done
20222017
instrs += BR_ON_CAST_FAIL(
20232018
doneLabel,
2024-
Types.WasmRefType(Types.WasmHeapType.ThrowableType),
2025-
Types.WasmRefType(Types.WasmHeapType.JSExceptionType)
2019+
Types.WasmRefType(genTypeName.ThrowableStruct),
2020+
Types.WasmRefType(genTypeName.JSExceptionStruct)
20262021
)
20272022

20282023
// otherwise, unwrap the JavaScriptException by reading its field
@@ -2486,7 +2481,7 @@ private class WasmExpressionBuilder private (
24862481

24872482
fctx.markPosition(t)
24882483

2489-
instrs += REF_CAST(Types.WasmRefType(Types.WasmHeapType.ObjectType))
2484+
instrs += REF_CAST(Types.WasmRefType(genTypeName.ObjectStruct))
24902485
instrs += LOCAL_TEE(expr)
24912486
instrs += REF_AS_NOT_NULL // cloneFunction argument is not nullable
24922487

Diff for: wasm/src/main/scala/wasm4s/Types.scala

-8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import org.scalajs.ir.{Names => IRNames}
44
import Names._
55
import Names.WasmTypeName._
66

7-
import wasm.ir2wasm.SpecialNames
8-
import wasm.ir2wasm.VarGen._
9-
107
object Types {
118
sealed trait WasmStorageType
129

@@ -93,10 +90,5 @@ object Types {
9390

9491
def apply(typ: WasmTypeName): WasmHeapType.Type =
9592
WasmHeapType.Type(typ)
96-
97-
val ObjectType = Type(genTypeName.forClass(IRNames.ObjectClass))
98-
val ClassType = Type(genTypeName.forClass(IRNames.ClassClass))
99-
val ThrowableType = Type(genTypeName.forClass(IRNames.ThrowableClass))
100-
val JSExceptionType = Type(genTypeName.forClass(SpecialNames.JSExceptionClass))
10193
}
10294
}

0 commit comments

Comments
 (0)