fieldHandling.scala 2.68 KB
Newer Older
1 2 3
package uvm.refimpl.mem.scanning

import uvm.refimpl.itpr.HasObjRef
Kunshan Wang's avatar
Kunshan Wang committed
4
import uvm.refimpl.itpr.BoxStack
5 6 7
import uvm.refimpl.mem.TypeSizes._
import uvm.refimpl.mem.MemorySupport
import uvm.refimpl.itpr.OpHelper
Kunshan Wang's avatar
Kunshan Wang committed
8 9 10
import uvm.refimpl.itpr.InterpreterThread
import uvm.refimpl.itpr.InterpreterStack
import uvm.refimpl.UvmRefImplException
11

Kunshan Wang's avatar
Kunshan Wang committed
12
/**
Kunshan Wang's avatar
Kunshan Wang committed
13 14 15 16 17
 * Handle references in the memory, value boxes or other Micro VM structures such as threads and stacks.
 * <ul>
 * <li>The caller invokes the methods on all boxes/locations it finds, no matter whether it is NULL or not.</li>
 * <li>The callee checks if the box/location actually contain non-null references.</li>
 * </ul>
Kunshan Wang's avatar
Kunshan Wang committed
18
 * <p>
Kunshan Wang's avatar
Kunshan Wang committed
19 20
 * The return value of the methods will be queued for recursive traversing. They should be the old value,
 * the updated value by the copying GC, or None if the reference should not be followed.
Kunshan Wang's avatar
Kunshan Wang committed
21
 */
22
trait RefFieldHandler {
Kunshan Wang's avatar
Kunshan Wang committed
23 24 25 26 27 28 29 30 31 32 33 34
  /** A stack value box referring to a heap object. */
  def boxToHeap(box: HasObjRef): Option[Word]
  /** A stack value box referring to a "stack"-typed value. */
  def boxToStack(box: BoxStack): Option[InterpreterStack]
  /** A memory location referring to a heap object. */
  def memToHeap(objRef: Word, iRef: Word, toObj: Word, isWeak: Boolean, isTR64: Boolean): Option[Word]
  /** A memory location referring to a heap object.  Return the new stack ID or 0. */
  def memToStack(objRef: Word, iRef: Word, toStack: Option[InterpreterStack]): Option[InterpreterStack]
  /** An InterpreterStack object referring to its stackMemory field. Stack memory cannot move. */
  def stackToStackMem(stack: InterpreterStack, toObj: Word): Option[Word]
  /** An InterpreterThread referring to its stack. GC cannot rebind stacks. */
  def threadToStack(thread: InterpreterThread, toStack: Option[InterpreterStack]): Option[InterpreterStack]
Kunshan Wang's avatar
Kunshan Wang committed
35 36
  /** Pin set referring to the memory. Pinned objects cannot be moved. */
  def pinSetToMem(toObj: Word): Option[Word]
37 38 39
}

object RefFieldUpdater {
Kunshan Wang's avatar
Kunshan Wang committed
40 41
  def updateBoxToHeap(box: HasObjRef, newObjRef: Word): Unit = box.setObjRef(newObjRef)
  def updateBoxToStack(box: BoxStack, newStack: Option[InterpreterStack]) = box.stack = newStack
42
  def updateMemToHeap(iRef: Word, isTR64: Boolean, newObjRef: Word)(implicit memorySupport: MemorySupport): Unit = {
43
    if (isTR64) {
44
      val oldRaw = memorySupport.loadLong(iRef)
45 46
      val oldTag = OpHelper.tr64ToTag(oldRaw)
      val newRaw = OpHelper.refToTr64(newObjRef, oldTag)
47
      memorySupport.storeLong(iRef, newRaw)
48
    } else {
49
      memorySupport.storeLong(iRef, newObjRef)
50 51
    }
  }
52 53
  def updateMemToStack(iRef: Word, newStack: Option[InterpreterStack])(implicit memorySupport: MemorySupport): Unit = {
    memorySupport.storeLong(iRef, newStack.map(_.id).getOrElse(0).toLong)
Kunshan Wang's avatar
Kunshan Wang committed
54
  }
55
}