forked from scala-native/scala-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCExternals.scala
55 lines (44 loc) · 1.64 KB
/
GCExternals.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)
}