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

Implement monomorphic Array #16

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/src/main/scala/TestSuites.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ object TestSuites {
val suites = List(
TestSuite("testsuite.core.Simple"),
TestSuite("testsuite.core.Add"),
TestSuite("testsuite.core.ArrayTest"),
TestSuite("testsuite.core.VirtualDispatch"),
TestSuite("testsuite.core.InterfaceCall"),
TestSuite("testsuite.core.AsInstanceOfTest"),
Expand Down
46 changes: 2 additions & 44 deletions sample/src/main/scala/Sample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@ import scala.annotation.tailrec
import scala.scalajs.js
import scala.scalajs.js.annotation._

//
// class Base {
// def sqrt(x: Int) = x * x
// }
//
object Main {
@JSExportTopLevel("test")
def test(i: Int): Boolean = {
val loopFib = fib(new LoopFib {}, i)
val recFib = fib(new RecFib {}, i)
val tailrecFib = fib(new TailRecFib {}, i)
js.Dynamic.global.console.log(s"loopFib: $loopFib -- recFib: $recFib -- tailrecFib: $tailrecFib")
js.Dynamic.global.console
.log(s"loopFib: $loopFib -- recFib: $recFib -- tailrecFib: $tailrecFib")
val date = new js.Date(0)
js.Dynamic.global.console.log(date)
loopFib == recFib && loopFib == tailrecFib
}
def fib(fib: Fib, n: Int): Int = fib.fib(n)
}


trait LoopFib extends Fib {
def fib(n: Int): Int = {
var a = 0
Expand Down Expand Up @@ -61,41 +56,4 @@ trait TailRecFib extends Fib {

trait Fib {
def fib(n: Int): Int
// = {
// if (n <= 1) {
// n
// } else {
// fib(n - 1) + fib(n - 2)
// }
// }

}

//
//
// object Bar {
// def bar(b: Base) = b.base
// }

// class Base extends Incr {
// override def incr(x: Int) = foo(x) + 1
// }
//
// trait Incr extends BaseTrait {
// // val one = 1
// def incr(x: Int): Int
// }
//
// trait BaseTrait {
// def foo(x: Int) = x
// }

// object Foo {
// def foo =
// Main.ident(1)
// }
//
// class Derived(override val i: Int) extends Base(i) {
// def derived(x: Int) = x * i
// override def base(x: Int): Int = x * i
// }
36 changes: 36 additions & 0 deletions test-suite/src/main/scala/testsuite/core/ArrayTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package testsuite.core

import testsuite.Assert

object ArrayTest {
def main(): Unit = {
Assert.ok(
testLength() && testSelect() && testNew()
)
}

def testLength(): Boolean = {
Array(1, 2, 3).length == 3 &&
(Array(Array(1, 2), Array(2), Array(3))).length == 3
}

def testSelect(): Boolean = {
val a = Array(Array(1), Array(2), Array(3))
a(0)(0) == 1 && {
a(0)(0) = 100 // Assign(ArraySelect(...), ...)
a(0)(0) == 100 // ArraySelect(...)
} && {
a(1) = Array(1, 2, 3)
a(1).length == 3 && a(1)(0) == 1
}
}

def testNew(): Boolean = {
(Array.emptyBooleanArray.length == 0) &&
(new Array[Int](10)).length == 10 &&
(new Array[Int](1))(0) == 0 &&
(new Array[Array[Array[Int]]](5))(0) == null
}

// TODO: Array.ofDim[T](...)
}
56 changes: 25 additions & 31 deletions wasm/src/main/scala/ir2wasm/TypeTransformer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object TypeTransformer {
def transformFunctionType(
// clazz: WasmContext.WasmClassInfo,
method: WasmContext.WasmFunctionInfo
)(implicit ctx: FunctionTypeWriterWasmContext): WasmFunctionType = {
)(implicit ctx: TypeDefinableWasmContext): WasmFunctionType = {
// val className = clazz.name
val name = method.name
val receiverType = makeReceiverType
Expand Down Expand Up @@ -43,41 +43,35 @@ object TypeTransformer {
t match {
case IRTypes.AnyType => Types.WasmAnyRef

case tpe @ IRTypes.ArrayType(IRTypes.ArrayTypeRef(elemType, size)) =>
// TODO
// val wasmElemTy =
// elemType match {
// case IRTypes.ClassRef(className) =>
// // val gcTypeSym = context.gcTypes.reference(Ident(className.nameString))
// Types.WasmRefType(Types.WasmHeapType.Type(Names.WasmGCTypeName.fromIR(className)))
// case IRTypes.PrimRef(tpe) =>
// transform(tpe)
// }
// val field = WasmStructField("TODO", wasmElemTy, isMutable = false)
// val arrayTySym =
// context.gcTypes.define(WasmArrayType(Names.WasmGCTypeName.fromIR(tpe), field))
// Types.WasmRefType(Types.WasmHeapType.Type(arrayTySym))
???
case clazz @ IRTypes.ClassType(className) =>
className match {
case _ =>
val info = ctx.getClassInfo(clazz.className)
if (info.isAncestorOfHijackedClass)
Types.WasmAnyRef
else if (info.isInterface)
Types.WasmRefNullType(Types.WasmHeapType.ObjectType)
else
Types.WasmRefNullType(
Types.WasmHeapType.Type(Names.WasmTypeName.WasmStructTypeName(className))
)
}
case IRTypes.RecordType(fields) => ???
case tpe: IRTypes.ArrayType =>
Types.WasmRefNullType(
Types.WasmHeapType.Type(Names.WasmTypeName.WasmArrayTypeName(tpe.arrayTypeRef))
)
case IRTypes.ClassType(className) => transformClassByName(className)
case IRTypes.RecordType(fields) => ???
case IRTypes.StringType | IRTypes.UndefType =>
Types.WasmRefType.any
case p: IRTypes.PrimTypeWithRef => transformPrimType(p)
}

def transformPrimType(
private def transformClassByName(
className: IRNames.ClassName
)(implicit ctx: ReadOnlyWasmContext): Types.WasmType = {
className match {
case _ =>
val info = ctx.getClassInfo(className)
if (info.isAncestorOfHijackedClass)
Types.WasmAnyRef
else if (info.isInterface)
Types.WasmRefNullType(Types.WasmHeapType.ObjectType)
else
Types.WasmRefNullType(
Types.WasmHeapType.Type(Names.WasmTypeName.WasmStructTypeName(className))
)
}
}

private def transformPrimType(
t: IRTypes.PrimTypeWithRef
): Types.WasmType =
t match {
Expand Down
Loading