Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GC Compiler changes #682

Merged
merged 3 commits into from Apr 29, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -19,7 +19,8 @@ object Driver {
inject.TraitDispatchTables,
inject.HasTrait,
inject.RuntimeTypeInformation,
inject.ClassStruct
inject.ClassStruct,
inject.GCExternals
)

private val fastOptPasses = Seq(
Expand Down
Expand Up @@ -94,8 +94,13 @@ object ClassHierarchy {
Val.Struct(Global.None, vtable)
def dynDispatchTableStruct =
Type.Struct(Global.None, Seq(Type.Int, Type.Ptr, Type.Ptr, Type.Ptr))
def size: Long =
MemoryLayout(Type.Ptr +: allfields.map(_.ty)).size

def refMapStruct = Type.Struct(Global.None, Seq(Type.Ptr))
def memoryLayout = MemoryLayout(Type.Ptr +: allfields.map(_.ty))

def refMap = Val.Const(Val.Array(Type.Long, memoryLayout.offsetArray))
def size = memoryLayout.size

def classStruct: Type.Struct = {
val data = allfields.map(_.ty)
val classStructName = name member "layout"
Expand All @@ -111,16 +116,21 @@ object ClassHierarchy {
Type.Long, // size
Type.Struct(Global.None, Seq(Type.Int, Type.Int)), // range
dynDispatchTableStruct,
vtableStruct))
refMapStruct,
vtableStruct)
)
override def typeValue: Val.Struct =
Val.Struct(
Global.None,
Seq(super.typeValue,
Val.Long(size),
Val.Struct(Global.None,
Seq(Val.Int(range.head), Val.Int(range.last))),
dynDispatchTableValue,
vtableValue)
Seq(
super.typeValue,
Val.Long(size),
Val.Struct(Global.None,
Seq(Val.Int(range.head), Val.Int(range.last))),
dynDispatchTableValue,
Val.Struct(Global.None, Seq(refMap)),
vtableValue
)
)
}

Expand Down
@@ -1,10 +1,20 @@
package scala.scalanative.optimizer.analysis

import scala.scalanative.nir.Type
import scala.scalanative.nir.Type.RefKind
import scala.scalanative.nir.{Type, Val}
import scala.scalanative.optimizer.analysis.MemoryLayout.PositionedType
import scala.scalanative.util.unsupported

final case class MemoryLayout(size: Long, tys: List[PositionedType])
final case class MemoryLayout(size: Long, tys: List[PositionedType]) {
lazy val offsetArray: Seq[Val] = {
val ptrOffsets =
tys.collect {
case MemoryLayout.Tpe(_, offset, _: RefKind) => Val.Long(offset)
}

ptrOffsets :+ Val.Long(-1)
}
}

object MemoryLayout {

Expand Down
@@ -0,0 +1,55 @@
package scala.scalanative.optimizer.inject

import scala.collection.mutable
import scala.scalanative.nir._
import scala.scalanative.optimizer.analysis.ClassHierarchy.Top
import scala.scalanative.optimizer.{Inject, InjectCompanion}
import scala.scalanative.tools.Config

class GCExternals(top: Top) extends Inject {
override def apply(buffer: mutable.Buffer[Defn]) = {
buffer ++= genModuleArray(buffer)
buffer += genObjectArrayId()
}

def genModuleArray(defns: mutable.Buffer[Defn]): Seq[Defn] = {
val modules = defns.filter(_.isInstanceOf[Defn.Module])

val moduleArray = Val.Array(Type.Ptr, modules.map {
case Defn.Module(_, clsName, _, _) =>
Val.Global(clsName member "value", Type.Ptr)
})
val moduleArrayVar =
Defn.Var(Attrs.None,
GCExternals.moduleArrayName,
Type.Array(Type.Ptr, modules.size),
moduleArray)

val moduleArraySizeVar =
Defn.Var(Attrs.None,
GCExternals.moduleArraySizeName,
Type.Int,
Val.Int(modules.size))

Seq(moduleArrayVar, moduleArraySizeVar)
}

def genObjectArrayId(): Defn.Var = {
val objectArray =
top.nodes(Global.Top("scala.scalanative.runtime.ObjectArray"))

Defn.Var(Attrs.None,
GCExternals.objectArrayIdName,
Type.Int,
Val.Int(objectArray.id))
}
}

object GCExternals extends InjectCompanion {
val moduleArrayName = Global.Top("__modules")
val moduleArraySizeName = Global.Top("__modules_size")

val objectArrayIdName = Global.Top("__object_array_id")

override def apply(config: Config, top: Top) = new GCExternals(top)
}
Expand Up @@ -24,7 +24,7 @@ class MethodLowering(implicit fresh: Fresh, top: Top) extends Pass {
Op.Elem(cls.typeStruct,
typeptr,
Seq(Val.Int(0),
Val.Int(4), // index of vtable in type struct
Val.Int(5), // index of vtable in type struct
Val.Int(meth.vindex))))

let(n, Op.Load(Type.Ptr, methptrptr))
Expand Down